@basic-ui/core 0.0.28 → 0.0.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. package/build/cjs/index.js +44 -21
  2. package/build/cjs/index.js.map +1 -1
  3. package/build/esm/FocusLock/useFocusLock.js +21 -7
  4. package/build/esm/FocusLock/useFocusLock.js.map +1 -1
  5. package/build/esm/Menu/Menu.js +0 -3
  6. package/build/esm/Menu/Menu.js.map +1 -1
  7. package/build/esm/Menu/MenuButton.js +7 -5
  8. package/build/esm/Menu/MenuButton.js.map +1 -1
  9. package/build/esm/Menu/MenuList.js +8 -5
  10. package/build/esm/Menu/MenuList.js.map +1 -1
  11. package/build/esm/Menu/context.d.ts +0 -1
  12. package/build/esm/Menu/context.js.map +1 -1
  13. package/build/esm/Tooltip/Tooltip.d.ts +1 -0
  14. package/build/esm/Tooltip/Tooltip.js +10 -3
  15. package/build/esm/Tooltip/Tooltip.js.map +1 -1
  16. package/build/esm/hooks/useId.d.ts +1 -0
  17. package/build/esm/hooks/useId.js.map +1 -1
  18. package/build/tsconfig.tsbuildinfo +11 -11
  19. package/package.json +4 -3
  20. package/src/Accordion/Accordion.story.tsx +72 -0
  21. package/src/Accordion/Accordion.tsx +51 -0
  22. package/src/Accordion/AccordionBody.tsx +53 -0
  23. package/src/Accordion/AccordionHeader.tsx +165 -0
  24. package/src/Accordion/AccordionItem.tsx +43 -0
  25. package/src/Accordion/context.ts +35 -0
  26. package/src/Accordion/index.ts +4 -0
  27. package/src/Accordion/scopeQuery.ts +7 -0
  28. package/src/Accordion/styles.css +21 -0
  29. package/src/CheckBox/CheckBox.tsx +41 -0
  30. package/src/CheckBox/index.ts +1 -0
  31. package/src/ComboBox/ComboBox.story.tsx +118 -0
  32. package/src/ComboBox/Combobox.tsx +153 -0
  33. package/src/ComboBox/ComboboxButton.tsx +60 -0
  34. package/src/ComboBox/ComboboxInput.tsx +178 -0
  35. package/src/ComboBox/ComboboxLabel.tsx +32 -0
  36. package/src/ComboBox/ComboboxList.tsx +47 -0
  37. package/src/ComboBox/ComboboxOption.tsx +107 -0
  38. package/src/ComboBox/ComboboxPopover.tsx +58 -0
  39. package/src/ComboBox/cities.ts +23194 -0
  40. package/src/ComboBox/context.ts +33 -0
  41. package/src/ComboBox/hooks.tsx +428 -0
  42. package/src/ComboBox/index.ts +8 -0
  43. package/src/ComboBox/makeHash.ts +19 -0
  44. package/src/ComboBox/scopeQuery.ts +6 -0
  45. package/src/ComboBox/styles.css +30 -0
  46. package/src/FocusLock/FocusLock.tsx +59 -0
  47. package/src/FocusLock/index.ts +1 -0
  48. package/src/FocusLock/tabUtils.ts +28 -0
  49. package/src/FocusLock/useFocusLock.ts +61 -0
  50. package/src/List/List.story.tsx +17 -0
  51. package/src/List/List.tsx +17 -0
  52. package/src/List/ListItem.tsx +23 -0
  53. package/src/List/context.ts +19 -0
  54. package/src/List/index.ts +2 -0
  55. package/src/Menu/.gitkeep +0 -0
  56. package/src/Menu/Menu.story.tsx +158 -0
  57. package/src/Menu/Menu.tsx +60 -0
  58. package/src/Menu/MenuButton.tsx +83 -0
  59. package/src/Menu/MenuItem.tsx +83 -0
  60. package/src/Menu/MenuList.tsx +201 -0
  61. package/src/Menu/MenuPopover.tsx +25 -0
  62. package/src/Menu/context.ts +32 -0
  63. package/src/Menu/index.ts +5 -0
  64. package/src/Menu/scope.ts +7 -0
  65. package/src/Menu/styles.css +42 -0
  66. package/src/Modal/Modal.story.tsx +242 -0
  67. package/src/Modal/Modal.tsx +42 -0
  68. package/src/Modal/ModalBackdrop.tsx +72 -0
  69. package/src/Modal/NavDrawer.story.tsx +157 -0
  70. package/src/Modal/index.ts +2 -0
  71. package/src/Modal/styles.css +46 -0
  72. package/src/Popover/.gitkeep +0 -0
  73. package/src/Popper/Popper.story.tsx +267 -0
  74. package/src/Popper/Popper.tsx +149 -0
  75. package/src/Popper/PopperArrow.tsx +36 -0
  76. package/src/Popper/context.ts +9 -0
  77. package/src/Popper/index.ts +3 -0
  78. package/src/Popper/styles.css +60 -0
  79. package/src/Portal/Portal.tsx +20 -0
  80. package/src/Portal/index.ts +1 -0
  81. package/src/RadioButton/RadioButton.story.tsx +73 -0
  82. package/src/RadioButton/RadioButton.tsx +48 -0
  83. package/src/RadioButton/RadioGroup.tsx +56 -0
  84. package/src/RadioButton/context.ts +19 -0
  85. package/src/RadioButton/index.ts +2 -0
  86. package/src/SkipNav/SkipNav.tsx +16 -0
  87. package/src/SkipNav/index.tsx +1 -0
  88. package/src/Spinner/Spinner.story.tsx +30 -0
  89. package/src/Spinner/Spinner.tsx +112 -0
  90. package/src/Spinner/SpinnerButton.tsx +48 -0
  91. package/src/Spinner/context.ts +21 -0
  92. package/src/Spinner/index.ts +2 -0
  93. package/src/Spinner/styles.css +23 -0
  94. package/src/Tabs/Tab.story.tsx +78 -0
  95. package/src/Tabs/Tab.tsx +131 -0
  96. package/src/Tabs/TabList.tsx +63 -0
  97. package/src/Tabs/TabPanel.tsx +52 -0
  98. package/src/Tabs/TabPanels.tsx +30 -0
  99. package/src/Tabs/Tabs.tsx +47 -0
  100. package/src/Tabs/context.ts +30 -0
  101. package/src/Tabs/index.tsx +5 -0
  102. package/src/Tabs/scopeQuery.ts +6 -0
  103. package/src/Tabs/styles.css +0 -0
  104. package/src/Tooltip/.gitkeep +0 -0
  105. package/src/Tooltip/Tooltip.story.tsx +43 -0
  106. package/src/Tooltip/Tooltip.tsx +48 -0
  107. package/src/Tooltip/index.ts +1 -0
  108. package/src/Tooltip/stateMachine.ts +185 -0
  109. package/src/Tooltip/useTooltip.ts +121 -0
  110. package/src/hooks/index.ts +14 -0
  111. package/src/hooks/useAutoFocus.ts +13 -0
  112. package/src/hooks/useChildrenCounter.ts +50 -0
  113. package/src/hooks/useControlledState.ts +37 -0
  114. package/src/hooks/useFocusReturn.ts +23 -0
  115. package/src/hooks/useFocusState.ts +28 -0
  116. package/src/hooks/useGestureHandlers.ts +217 -0
  117. package/src/hooks/useId.ts +18 -0
  118. package/src/hooks/useMeasure.ts +33 -0
  119. package/src/hooks/useOnClickOutside.ts +32 -0
  120. package/src/hooks/useOnKeyDown.ts +18 -0
  121. package/src/hooks/useReducerMachine.ts +59 -0
  122. package/src/hooks/useRemoveBodyScroll.ts +37 -0
  123. package/src/hooks/useScope.ts +51 -0
  124. package/src/hooks/useThrottle.ts +19 -0
  125. package/src/index.ts +19 -0
  126. package/src/utils/assignRef.ts +27 -0
  127. package/src/utils/clamp.ts +3 -0
  128. package/src/utils/createSubscription.ts +16 -0
  129. package/src/utils/getCircularIndex.ts +7 -0
  130. package/src/utils/index.ts +4 -0
  131. package/src/utils/rubberBandClamp.ts +25 -0
  132. package/src/utils/wrapEvent.ts +20 -0
@@ -0,0 +1,28 @@
1
+ import { useState } from 'react';
2
+ import { wrapEvent } from '../utils/wrapEvent';
3
+
4
+ export function useFocusState<T>(
5
+ props: {
6
+ onFocus?: React.FocusEventHandler<T>;
7
+ onBlur?: React.FocusEventHandler<T>;
8
+ } = {}
9
+ ) {
10
+ const { onFocus, onBlur } = props;
11
+ const [hasFocus, setHasFocus] = useState(false);
12
+
13
+ const handleFocus = () => {
14
+ setHasFocus(true);
15
+ };
16
+
17
+ const handleBlur = () => {
18
+ setHasFocus(false);
19
+ };
20
+
21
+ return {
22
+ bind: {
23
+ onFocus: wrapEvent(onFocus, handleFocus),
24
+ onBlur: wrapEvent(onBlur, handleBlur),
25
+ },
26
+ hasFocus,
27
+ };
28
+ }
@@ -0,0 +1,217 @@
1
+ import { useRef } from 'react';
2
+ import type * as React from 'react';
3
+
4
+ export interface GestureHandlersState {
5
+ target: null | EventTarget;
6
+ x: number;
7
+ xDelta: number;
8
+ xDeltaPercent: number;
9
+ xInitial: number;
10
+ xPrev: number;
11
+ xVelocity: number;
12
+ y: number;
13
+ yDelta: number;
14
+ yDeltaPercent: number;
15
+ yInitial: number;
16
+ yPrev: number;
17
+ yVelocity: number;
18
+ down: boolean;
19
+ }
20
+
21
+ type SetStateFunc<S> = (
22
+ state: (prevState: Readonly<S>) => S,
23
+ callback?: () => void
24
+ ) => void;
25
+
26
+ export interface GestureHandlersReturn {
27
+ onMouseDown: (e: React.MouseEvent<HTMLElement>) => void;
28
+ onTouchStart: (e: React.TouchEvent<HTMLElement>) => void;
29
+ }
30
+
31
+ export interface GestureHandlerOptions {
32
+ ensureTargetIsContainer?: boolean;
33
+ }
34
+
35
+ export const initialGestureHandlersState: GestureHandlersState = {
36
+ target: null,
37
+ x: 0,
38
+ xDelta: 0,
39
+ xDeltaPercent: 0,
40
+ xInitial: 0,
41
+ xPrev: 0,
42
+ xVelocity: 0,
43
+ y: 0,
44
+ yDelta: 0,
45
+ yDeltaPercent: 0,
46
+ yInitial: 0,
47
+ yPrev: 0,
48
+ yVelocity: 0,
49
+ down: false,
50
+ };
51
+
52
+ export function gestureHandlers(
53
+ set: SetStateFunc<GestureHandlersState>,
54
+ containerRef?: React.MutableRefObject<HTMLElement | null>,
55
+ options: GestureHandlerOptions = {}
56
+ ): GestureHandlersReturn {
57
+ const { ensureTargetIsContainer = false } = options;
58
+
59
+ // Common handlers
60
+ const handleUp = () => {
61
+ set((state: GestureHandlersState) => {
62
+ const newState: GestureHandlersState = {
63
+ ...state,
64
+ target: null,
65
+ down: false,
66
+ };
67
+
68
+ return newState;
69
+ });
70
+ };
71
+
72
+ const handleDown = (e: MouseEvent) => {
73
+ const { target, pageX, pageY } = e;
74
+
75
+ set((state: GestureHandlersState) => {
76
+ const newState = {
77
+ ...state,
78
+ target,
79
+ x: pageX,
80
+ xDelta: 0,
81
+ xDeltaPercent: 0,
82
+ xVelocity: 0,
83
+ xInitial: pageX,
84
+ xPrev: pageX,
85
+ y: pageY,
86
+ yDelta: 0,
87
+ yDeltaPercent: 0,
88
+ yVelocity: 0,
89
+ yInitial: pageY,
90
+ yPrev: pageY,
91
+ down: true,
92
+ };
93
+
94
+ return newState;
95
+ });
96
+ };
97
+
98
+ function handleMove(e: MouseEvent) {
99
+ const { pageX, movementX, pageY, movementY } = e;
100
+ e.preventDefault && e.preventDefault(); // prevent drag & drop behaviour from browser
101
+
102
+ set((state: GestureHandlersState) => {
103
+ const target =
104
+ (containerRef && containerRef.current) || (e as any).target;
105
+
106
+ const width = target ? target.offsetWidth : NaN;
107
+ const xDelta = pageX - state.xInitial;
108
+ const xDeltaPercent = (xDelta * 100) / width;
109
+
110
+ const height = target ? target.offsetHeight : NaN;
111
+ const yDelta = pageY - state.yInitial;
112
+ const yDeltaPercent = (yDelta * 100) / height;
113
+
114
+ const newState = {
115
+ ...state,
116
+ xDelta,
117
+ xDeltaPercent,
118
+ x: pageX,
119
+ xPrev: state.x,
120
+ xVelocity: movementX,
121
+ yDelta,
122
+ yDeltaPercent,
123
+ y: pageY,
124
+ yPrev: state.y,
125
+ yVelocity: movementY,
126
+ };
127
+
128
+ return newState;
129
+ });
130
+ }
131
+
132
+ // Touch handlers
133
+
134
+ /* eslint-disable @typescript-eslint/no-use-before-define */
135
+ function handleTouchMove(e: TouchEvent) {
136
+ e.preventDefault();
137
+ handleMove(e.touches.item(0) as any);
138
+ }
139
+
140
+ function handleTouchStart(e: React.TouchEvent<HTMLElement>) {
141
+ // making sure we're not dragging the element when more than one finger press the screen
142
+ const { touches } = e;
143
+ if (touches.length > 1) {
144
+ return;
145
+ }
146
+
147
+ if (
148
+ ensureTargetIsContainer &&
149
+ containerRef &&
150
+ e.target !== containerRef.current
151
+ ) {
152
+ return;
153
+ }
154
+
155
+ window.addEventListener('touchmove', handleTouchMove, { passive: false });
156
+ window.addEventListener('touchend', handleTouchEnd);
157
+ window.addEventListener('touchcancel', handleTouchEnd);
158
+ handleDown(e.touches.item(0) as any);
159
+ }
160
+
161
+ function handleTouchEnd() {
162
+ window.removeEventListener('touchmove', handleTouchMove);
163
+ window.removeEventListener('touchend', handleTouchEnd);
164
+ window.removeEventListener('touchcancel', handleTouchEnd);
165
+ handleUp();
166
+ }
167
+
168
+ // Mouse handlers
169
+ function handleMouseDown(e: React.MouseEvent<HTMLElement>) {
170
+ if (
171
+ ensureTargetIsContainer &&
172
+ containerRef &&
173
+ e.target !== containerRef.current
174
+ ) {
175
+ return;
176
+ }
177
+
178
+ if (e.button === 0) {
179
+ window.addEventListener('mousemove', handleMove);
180
+ window.addEventListener('mouseup', handleMouseUp);
181
+ handleDown(e as any);
182
+ }
183
+ }
184
+
185
+ function handleMouseUp() {
186
+ window.removeEventListener('mousemove', handleMove);
187
+ window.removeEventListener('mouseup', handleMouseUp);
188
+ handleUp();
189
+ }
190
+ /* eslint-enable @typescript-eslint/no-use-before-define */
191
+
192
+ return {
193
+ onMouseDown: handleMouseDown,
194
+ onTouchStart: handleTouchStart,
195
+ };
196
+ }
197
+
198
+ export const useGestureHandlers = (
199
+ containerRef: React.MutableRefObject<HTMLElement | null> | undefined,
200
+ onGesture: (e: GestureHandlersState) => void,
201
+ options: GestureHandlerOptions = {}
202
+ ) => {
203
+ const state = useRef({ ...initialGestureHandlersState });
204
+
205
+ const set = (
206
+ cb: (prevState: GestureHandlersState) => GestureHandlersState
207
+ ) => {
208
+ state.current = cb(state.current);
209
+ onGesture && onGesture(state.current);
210
+
211
+ return state.current;
212
+ };
213
+
214
+ const handlers = gestureHandlers(set, containerRef, options);
215
+
216
+ return { state: state.current, handlers };
217
+ };
@@ -0,0 +1,18 @@
1
+ import { useEffect, useState } from 'react';
2
+
3
+ // Could use UUID but if we hit 9,007,199,254,740,991 unique components over
4
+ // the lifetime of the app before it gets reloaded, I mean ... come on.
5
+ // I don't even know what xillion that is.
6
+ // /me googles
7
+ // Oh duh, quadrillion. Nine quadrillion components. I think we're okay.
8
+ let id = 0;
9
+ const genId = () => `${++id}`;
10
+
11
+ export function useId(): string | undefined;
12
+ export function useId(preferredId: string): string;
13
+ export function useId(preferredId: string | undefined): string | undefined;
14
+ export function useId(preferredId?: string): string | undefined {
15
+ const [id, setId] = useState<string | undefined>(preferredId);
16
+ useEffect(() => setId(genId()), []);
17
+ return id;
18
+ }
@@ -0,0 +1,33 @@
1
+ import { useRef, useState, useEffect } from 'react';
2
+ import ResizeObserver from 'resize-observer-polyfill';
3
+
4
+ export function useMeasure(ref: React.MutableRefObject<HTMLElement | null>) {
5
+ const ro = useRef<ResizeObserver | null>(null);
6
+ const [bounds, setBounds] = useState({
7
+ left: 0,
8
+ top: 0,
9
+ width: 0,
10
+ height: 0,
11
+ });
12
+
13
+ useEffect(() => {
14
+ if (ro.current === null) {
15
+ ro.current = new ResizeObserver((entries: any) => {
16
+ const entry = entries[0];
17
+ setBounds(entry.contentRect);
18
+ });
19
+ }
20
+
21
+ if (ref.current) {
22
+ ro.current.observe(ref.current);
23
+ }
24
+
25
+ return () => {
26
+ if (ro.current) {
27
+ ro.current.disconnect();
28
+ }
29
+ };
30
+ }, [ref]);
31
+
32
+ return bounds;
33
+ }
@@ -0,0 +1,32 @@
1
+ import { useEffect, useCallback } from 'react';
2
+ import type * as React from 'react';
3
+
4
+ export function useOnClickOutside(
5
+ ref: React.RefObject<any>,
6
+ handler: (e: PointerEvent) => void,
7
+ active = true
8
+ ) {
9
+ const listener = useCallback(
10
+ (event: PointerEvent) => {
11
+ // Do nothing if clicking ref's element or descendent elements
12
+ if (!ref.current || ref.current.contains(event.target)) {
13
+ return;
14
+ }
15
+
16
+ handler && handler(event);
17
+ },
18
+ [ref, handler]
19
+ );
20
+
21
+ useEffect(() => {
22
+ if (active) {
23
+ document.addEventListener('pointerup', listener);
24
+
25
+ return () => {
26
+ document.removeEventListener('pointerup', listener);
27
+ };
28
+ }
29
+
30
+ return;
31
+ }, [listener, active]);
32
+ }
@@ -0,0 +1,18 @@
1
+ import { useEffect } from 'react';
2
+
3
+ export function useOnKeyDown(
4
+ handler: (e: KeyboardEvent) => void,
5
+ active = true
6
+ ) {
7
+ useEffect(() => {
8
+ if (active) {
9
+ window.addEventListener('keydown', handler);
10
+
11
+ return () => {
12
+ window.removeEventListener('keydown', handler);
13
+ };
14
+ }
15
+
16
+ return;
17
+ }, [active, handler]);
18
+ }
@@ -0,0 +1,59 @@
1
+ import { useReducer, Reducer } from 'react';
2
+
3
+ export interface StateChart<STypes extends string, ATypes extends string> {
4
+ initial: STypes;
5
+ states: {
6
+ [state in STypes]?: {
7
+ enter?: (prevState: STypes, payload: any) => void;
8
+ leave?: (nextState: STypes, payload: any) => void;
9
+ on: { [action in ATypes]?: STypes };
10
+ };
11
+ };
12
+ }
13
+
14
+ export interface StateMachineState<STypes, ATypes> {
15
+ state: STypes;
16
+ lastActionType: ATypes;
17
+ }
18
+
19
+ export interface StateMachineAction<STypes, ATypes> {
20
+ type: ATypes;
21
+ nextState: STypes;
22
+ }
23
+
24
+ type TransitionType<ATypes extends string> = (
25
+ action: ATypes,
26
+ payload: any
27
+ ) => void;
28
+
29
+ // This manages transitions between states with a built in reducer to manage
30
+ // the data that goes with those transitions.
31
+ export function useReducerMachine<
32
+ S extends StateMachineState<STypes, ATypes>,
33
+ A extends StateMachineAction<STypes, ATypes>,
34
+ STypes extends string,
35
+ ATypes extends string
36
+ >(
37
+ chart: StateChart<STypes, ATypes>,
38
+ reducer: Reducer<S, A>,
39
+ initialData: S
40
+ ): [STypes, Omit<S, 'state'>, TransitionType<ATypes>] {
41
+ const [reducerState, dispatch] = useReducer(reducer, initialData);
42
+ const { state, ...data } = reducerState;
43
+
44
+ const transition: TransitionType<ATypes> = (action, payload = {}) => {
45
+ const currentState = chart.states[state];
46
+ if (!currentState) {
47
+ throw new Error(`Unknown currentState "${state}"`);
48
+ }
49
+
50
+ const nextState: STypes | undefined = currentState.on[action];
51
+ if (!nextState) {
52
+ throw new Error(`Unknown action "${action}" for state "${state}"`);
53
+ }
54
+
55
+ dispatch({ type: action, nextState, ...payload } as any);
56
+ };
57
+
58
+ return [state, data, transition];
59
+ }
@@ -0,0 +1,37 @@
1
+ import { useEffect } from 'react';
2
+
3
+ export function useRemoveBodyScroll(open: boolean) {
4
+ useEffect(() => {
5
+ if (open) {
6
+ const locksCount = ((window as any).__SCROLL_BODY_COUNT__ || 0) + 1;
7
+
8
+ // calculate scrollbar width if mounting the first scroll lock
9
+ let scrollBarWidth = 0;
10
+ if (locksCount === 1) {
11
+ scrollBarWidth = window.innerWidth - document.body.clientWidth;
12
+ }
13
+
14
+ document.body.style.overflow = 'hidden';
15
+ if (scrollBarWidth > 0) {
16
+ document.body.style.paddingRight = `${scrollBarWidth}px`;
17
+ }
18
+
19
+ // save current lock count in global object
20
+ (window as any).__SCROLL_BODY_COUNT__ = locksCount;
21
+ }
22
+
23
+ return () => {
24
+ if (open) {
25
+ const locksCount = (window as any).__SCROLL_BODY_COUNT__ - 1;
26
+
27
+ if (locksCount === 0) {
28
+ document.body.style.overflow = '';
29
+ document.body.style.paddingRight = '';
30
+ }
31
+
32
+ // save current lock count in global object
33
+ (window as any).__SCROLL_BODY_COUNT__ = locksCount;
34
+ }
35
+ };
36
+ }, [open]);
37
+ }
@@ -0,0 +1,51 @@
1
+ import { useRef, MutableRefObject } from 'react';
2
+
3
+ export type ScopeMatcherFn = (
4
+ nodeType: string,
5
+ props: {
6
+ [key: string]: string;
7
+ },
8
+ instance: Element
9
+ ) => boolean;
10
+
11
+ export type Scope<T extends HTMLElement> = MutableRefObject<{
12
+ queryAllNodes: (matcherFn: ScopeMatcherFn) => T[];
13
+ }>;
14
+
15
+ export function getScope<T extends HTMLElement, R extends HTMLElement>(
16
+ rootRef: MutableRefObject<R | undefined | null>
17
+ ) {
18
+ const queryAllNodes = (matcherFn: ScopeMatcherFn) => {
19
+ if (!rootRef.current) {
20
+ return [];
21
+ }
22
+
23
+ const allNodes = rootRef.current.querySelectorAll('*');
24
+
25
+ const filtered: T[] = [];
26
+ allNodes.forEach((node) => {
27
+ const props = {};
28
+ const { attributes } = node;
29
+ for (let i = 0; i < attributes.length; i++) {
30
+ const attr = attributes[i];
31
+ props[attr.name] = attr.value;
32
+ }
33
+
34
+ if (matcherFn(node.tagName.toLowerCase(), props, node)) {
35
+ filtered.push(node as T);
36
+ }
37
+ });
38
+
39
+ return filtered;
40
+ };
41
+
42
+ return { queryAllNodes };
43
+ }
44
+
45
+ export function useScope<T extends HTMLElement, R extends HTMLElement>(
46
+ rootRef: MutableRefObject<R | undefined | null>
47
+ ): Scope<T> {
48
+ const scope: Scope<T> = useRef(getScope(rootRef));
49
+
50
+ return scope;
51
+ }
@@ -0,0 +1,19 @@
1
+ import { useState, useEffect, useRef } from 'react';
2
+
3
+ export function useThrottle<T>(value: T, limit: number) {
4
+ const [throttledValue, setThrottledValue] = useState(value);
5
+ const lastRan = useRef(Date.now());
6
+
7
+ useEffect(() => {
8
+ const handler = setTimeout(() => {
9
+ setThrottledValue(value);
10
+ lastRan.current = Date.now();
11
+ }, limit - (Date.now() - lastRan.current));
12
+
13
+ return () => {
14
+ clearTimeout(handler);
15
+ };
16
+ }, [value, limit]);
17
+
18
+ return throttledValue;
19
+ }
package/src/index.ts ADDED
@@ -0,0 +1,19 @@
1
+ // Components
2
+ export * from './Accordion';
3
+ export * from './CheckBox';
4
+ export * from './ComboBox';
5
+ export * from './FocusLock';
6
+ export * from './Menu';
7
+ export * from './Modal';
8
+ export * from './Popper';
9
+ export * from './Portal';
10
+ export * from './RadioButton';
11
+ export * from './Spinner';
12
+ export * from './Tabs';
13
+ export * from './Tooltip';
14
+
15
+ // General React utilities
16
+ export * from './utils';
17
+
18
+ // Hooks
19
+ export * from './hooks';
@@ -0,0 +1,27 @@
1
+ import { LegacyRef, MutableRefObject, Ref, RefCallback } from 'react';
2
+
3
+ export function assignRef<T>(
4
+ ref: LegacyRef<T> | MutableRefObject<T> | Ref<T> | null | undefined,
5
+ value: T
6
+ ) {
7
+ if (ref == null) return;
8
+ if (typeof ref === 'function') {
9
+ ref(value);
10
+ } else {
11
+ try {
12
+ (ref as MutableRefObject<T>).current = value;
13
+ } catch (error) {
14
+ throw new Error(`Cannot assign value "${value}" to ref "${ref}"`);
15
+ }
16
+ }
17
+ }
18
+
19
+ export function assignMultipleRefs<T>(
20
+ ...refs: (LegacyRef<T> | MutableRefObject<T> | Ref<T> | null | undefined)[]
21
+ ): RefCallback<T> {
22
+ return (node: T | null) => {
23
+ refs.forEach((ref) => {
24
+ assignRef(ref, node);
25
+ });
26
+ };
27
+ }
@@ -0,0 +1,3 @@
1
+ export function clamp(value: number, min: number, max: number) {
2
+ return Math.min(Math.max(value, min), max);
3
+ }
@@ -0,0 +1,16 @@
1
+ export function createSubscription() {
2
+ const subscriptions: (() => void)[] = [];
3
+
4
+ function subscribe(fn: () => void) {
5
+ subscriptions.push(fn);
6
+ return () => {
7
+ subscriptions.splice(subscriptions.indexOf(fn), 1);
8
+ };
9
+ }
10
+
11
+ function notify() {
12
+ subscriptions.forEach((fn) => fn());
13
+ }
14
+
15
+ return { subscribe, notify };
16
+ }
@@ -0,0 +1,7 @@
1
+ export function getCircularIndex(index: number, maxLength: number) {
2
+ if (maxLength < 0) {
3
+ return null;
4
+ }
5
+
6
+ return ((index % maxLength) + maxLength) % maxLength;
7
+ }
@@ -0,0 +1,4 @@
1
+ export * from './assignRef';
2
+ export * from './wrapEvent';
3
+ export * from './getCircularIndex';
4
+ export * from './rubberBandClamp';
@@ -0,0 +1,25 @@
1
+ // https://twitter.com/chpwn/status/285540192096497664
2
+ // iOS constant = 0.55
3
+ export function rubberBand(
4
+ distance: number,
5
+ dimension: number,
6
+ constant = 0.15
7
+ ) {
8
+ return (distance * dimension * constant) / (dimension + constant * distance);
9
+ }
10
+
11
+ export function rubberBandClamp(
12
+ min: number,
13
+ max: number,
14
+ delta: number,
15
+ constant = 0.15
16
+ ) {
17
+ if (delta < min) {
18
+ return -rubberBand(min - delta, max - min, constant) + min;
19
+ }
20
+ if (delta > max) {
21
+ return rubberBand(delta - max, max - min, constant) + max;
22
+ }
23
+
24
+ return delta;
25
+ }
@@ -0,0 +1,20 @@
1
+ export type CustomEventHandler<
2
+ E extends React.SyntheticEvent<any>,
3
+ H extends unknown[]
4
+ > = (e: E, ...args: H) => void;
5
+
6
+ export function wrapEvent<
7
+ E extends React.SyntheticEvent<any>,
8
+ H extends unknown[]
9
+ >(
10
+ theirHandler: CustomEventHandler<E, H> | undefined,
11
+ ourHandler: CustomEventHandler<E, H>
12
+ ): CustomEventHandler<E, H> {
13
+ return (event: E, ...otherArgs: H) => {
14
+ const ret = theirHandler && theirHandler(event, ...otherArgs);
15
+ if (!event.defaultPrevented) {
16
+ return ourHandler(event, ...otherArgs);
17
+ }
18
+ return ret;
19
+ };
20
+ }