TwitterGitHub
Dark LogoStratik / UIbeta
useDisableScroll

The useDisableScroll hook is used to disable scrolling on a web page or a specific element. This hook is particularly useful in scenarios where you want to prevent the user from scrolling, such as when a modal or a full-screen menu is open.

Parameters
NameTypeDefaultDescription
isModalOpenboolean-A flag indicating whether the modal is open, which determines if scrolling should be disabled on the target element.
referencestring | RefObject<HTMLElement>document.bodyAn optional reference to the HTML element to apply scroll behavior. Can be an element ID as a string or a React ref object. If not provided, defaults to the document.body.

Additional Information

The useDisableScroll hook is designed to manage the scrolling behavior of a specified HTML element when a modal or other overlay component is open. When isModalOpen is true, scrolling is disabled on the target element by setting its overflow style to "hidden". When isModalOpen is false, scrolling is enabled by setting overflow back to "auto".

  • Element Selection: The target element for disabling scroll can be specified through the reference parameter. This can either be:
    • A string representing the element's ID.
    • A RefObject pointing to the desired element.
    • If no reference is provided, the default target is document.body.

This is a scrollable div

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100