@contember/react-utils 1.2.0-rc.8 → 1.2.0
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.
- package/dist/development/hooks/useAutoHeightTextArea.js +41 -0
- package/dist/development/hooks/useAutoHeightTextArea.js.map +1 -0
- package/dist/development/hooks/useContainerWidth.js +1 -1
- package/dist/development/hooks/useContainerWidth.js.map +1 -1
- package/dist/development/hooks/useElementTopOffset.js +29 -0
- package/dist/development/hooks/useElementTopOffset.js.map +1 -0
- package/dist/development/hooks/useExpectSameValueReference.js +1 -1
- package/dist/development/hooks/useExpectSameValueReference.js.map +1 -1
- package/dist/development/hooks/useId.js +2 -2
- package/dist/development/hooks/useId.js.map +1 -1
- package/dist/development/hooks/useOnElementClickOutsideCallback.js +20 -0
- package/dist/development/hooks/useOnElementClickOutsideCallback.js.map +1 -0
- package/dist/development/hooks/useOnElementMouseEnterDelayedCallback.js +33 -0
- package/dist/development/hooks/useOnElementMouseEnterDelayedCallback.js.map +1 -0
- package/dist/development/hooks/useOnElementMutation.js +1 -1
- package/dist/development/hooks/useOnElementMutation.js.map +1 -1
- package/dist/development/hooks/useOnElementResize.js +28 -26
- package/dist/development/hooks/useOnElementResize.js.map +1 -1
- package/dist/development/hooks/useOnScrollWithin.js +33 -31
- package/dist/development/hooks/useOnScrollWithin.js.map +1 -1
- package/dist/development/index.js +10 -0
- package/dist/development/index.js.map +1 -1
- package/dist/development/theming/useClassNameFactory.js +4 -2
- package/dist/development/theming/useClassNameFactory.js.map +1 -1
- package/dist/development/theming/useThemedClassNameFactory.js +4 -2
- package/dist/development/theming/useThemedClassNameFactory.js.map +1 -1
- package/dist/production/hooks/useAutoHeightTextArea.js +41 -0
- package/dist/production/hooks/useAutoHeightTextArea.js.map +1 -0
- package/dist/production/hooks/useContainerWidth.js +1 -1
- package/dist/production/hooks/useContainerWidth.js.map +1 -1
- package/dist/production/hooks/useElementTopOffset.js +29 -0
- package/dist/production/hooks/useElementTopOffset.js.map +1 -0
- package/dist/production/hooks/useExpectSameValueReference.js +1 -1
- package/dist/production/hooks/useExpectSameValueReference.js.map +1 -1
- package/dist/production/hooks/useId.js +2 -2
- package/dist/production/hooks/useId.js.map +1 -1
- package/dist/production/hooks/useOnElementClickOutsideCallback.js +20 -0
- package/dist/production/hooks/useOnElementClickOutsideCallback.js.map +1 -0
- package/dist/production/hooks/useOnElementMouseEnterDelayedCallback.js +33 -0
- package/dist/production/hooks/useOnElementMouseEnterDelayedCallback.js.map +1 -0
- package/dist/production/hooks/useOnElementMutation.js +1 -1
- package/dist/production/hooks/useOnElementMutation.js.map +1 -1
- package/dist/production/hooks/useOnElementResize.js +28 -26
- package/dist/production/hooks/useOnElementResize.js.map +1 -1
- package/dist/production/hooks/useOnScrollWithin.js +33 -31
- package/dist/production/hooks/useOnScrollWithin.js.map +1 -1
- package/dist/production/index.js +10 -0
- package/dist/production/index.js.map +1 -1
- package/dist/production/theming/useClassNameFactory.js +4 -2
- package/dist/production/theming/useClassNameFactory.js.map +1 -1
- package/dist/production/theming/useThemedClassNameFactory.js +4 -2
- package/dist/production/theming/useThemedClassNameFactory.js.map +1 -1
- package/dist/types/hooks/index.d.ts +5 -0
- package/dist/types/hooks/index.d.ts.map +1 -1
- package/dist/types/hooks/useAutoHeightTextArea.d.ts +3 -0
- package/dist/types/hooks/useAutoHeightTextArea.d.ts.map +1 -0
- package/dist/types/hooks/useContainerWidth.d.ts +1 -1
- package/dist/types/hooks/useContainerWidth.d.ts.map +1 -1
- package/dist/types/hooks/useElementTopOffset.d.ts +3 -0
- package/dist/types/hooks/useElementTopOffset.d.ts.map +1 -0
- package/dist/types/hooks/useOnElementClickOutsideCallback.d.ts +3 -0
- package/dist/types/hooks/useOnElementClickOutsideCallback.d.ts.map +1 -0
- package/dist/types/hooks/useOnElementMouseEnterDelayedCallback.d.ts +14 -0
- package/dist/types/hooks/useOnElementMouseEnterDelayedCallback.d.ts.map +1 -0
- package/dist/types/hooks/useOnElementResize.d.ts.map +1 -1
- package/dist/types/hooks/useOnScrollWithin.d.ts.map +1 -1
- package/dist/types/theming/useClassNameFactory.d.ts.map +1 -1
- package/dist/types/theming/useThemedClassNameFactory.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -5
- package/src/hooks/index.ts +5 -0
- package/src/hooks/useAutoHeightTextArea.ts +51 -0
- package/src/hooks/useContainerWidth.ts +1 -1
- package/src/hooks/useElementTopOffset.ts +35 -0
- package/src/hooks/useExpectSameValueReference.ts +1 -1
- package/src/hooks/useId.ts +2 -2
- package/src/hooks/useOnElementClickOutsideCallback.ts +23 -0
- package/src/hooks/useOnElementMouseEnterDelayedCallback.ts +54 -0
- package/src/hooks/useOnElementMutation.ts +1 -1
- package/src/hooks/useOnElementResize.ts +34 -31
- package/src/hooks/useOnScrollWithin.ts +33 -32
- package/src/theming/useClassNameFactory.ts +4 -1
- package/src/theming/useThemedClassNameFactory.ts +4 -1
|
@@ -11,42 +11,45 @@ export function useOnElementResize(
|
|
|
11
11
|
const scopedConsoleRef = useScopedConsoleRef('useOnElementResize')
|
|
12
12
|
|
|
13
13
|
const { box = 'border-box' } = options
|
|
14
|
-
const element = unwrapRefValue(refOrElement)
|
|
15
14
|
const callbackRef = useRef(callback); callbackRef.current = callback
|
|
16
15
|
const lastTimeStamp = useRef<number>(0)
|
|
17
16
|
|
|
18
17
|
useLayoutEffect(() => {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
18
|
+
const element = unwrapRefValue(refOrElement)
|
|
19
|
+
|
|
20
|
+
if (element) {
|
|
21
|
+
if (element instanceof HTMLElement) {
|
|
22
|
+
let timeoutID: number | undefined = undefined
|
|
23
|
+
|
|
24
|
+
function debouncedOnChange([entry]: ResizeObserverEntry[]) {
|
|
25
|
+
const timeStamp = Date.now()
|
|
26
|
+
const delta = timeStamp - lastTimeStamp.current
|
|
27
|
+
|
|
28
|
+
if (delta > timeout) {
|
|
29
|
+
scopedConsoleRef.current.warned('element.resize:immediate', null)
|
|
30
|
+
callbackRef.current(entry)
|
|
31
|
+
lastTimeStamp.current = timeStamp
|
|
32
|
+
} else {
|
|
33
|
+
clearTimeout(timeoutID)
|
|
34
|
+
timeoutID = setTimeout(() => {
|
|
35
|
+
scopedConsoleRef.current.warned('element.resize:debounced', null)
|
|
36
|
+
callbackRef.current(entry)
|
|
37
|
+
lastTimeStamp.current = timeStamp
|
|
38
|
+
}, timeout)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const resizeObserver = new ResizeObserver(debouncedOnChange)
|
|
43
|
+
|
|
44
|
+
resizeObserver.observe(element, { box })
|
|
45
|
+
|
|
46
|
+
return () => {
|
|
47
|
+
clearTimeout(timeoutID)
|
|
48
|
+
resizeObserver.unobserve(element)
|
|
49
|
+
}
|
|
29
50
|
} else {
|
|
30
|
-
|
|
31
|
-
timeoutID = setTimeout(() => {
|
|
32
|
-
scopedConsoleRef.current.warned('element.resize:debounced', null)
|
|
33
|
-
callbackRef.current(entry)
|
|
34
|
-
lastTimeStamp.current = timeStamp
|
|
35
|
-
}, timeout)
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if (element instanceof HTMLElement) {
|
|
40
|
-
const resizeObserver = new ResizeObserver(debouncedOnChange)
|
|
41
|
-
|
|
42
|
-
resizeObserver.observe(element, { box })
|
|
43
|
-
|
|
44
|
-
return () => {
|
|
45
|
-
clearTimeout(timeoutID)
|
|
46
|
-
resizeObserver.unobserve(element)
|
|
51
|
+
throw new Error('Exhaustive error: Expecting element to be instance of HTMLElement')
|
|
47
52
|
}
|
|
48
|
-
} else if (element) {
|
|
49
|
-
throw new Error('Exhaustive error: Expecting element to be instance of HTMLElement')
|
|
50
53
|
}
|
|
51
|
-
}, [box,
|
|
54
|
+
}, [box, refOrElement, scopedConsoleRef, timeout])
|
|
52
55
|
}
|
|
@@ -19,49 +19,50 @@ export function useOnScrollWithin(
|
|
|
19
19
|
const callbackRef = useRef(callback); callbackRef.current = callback
|
|
20
20
|
const lastTimeStamp = useRef<number>(0)
|
|
21
21
|
|
|
22
|
-
const element = unwrapRefValue(refOrElement)
|
|
23
|
-
|
|
24
22
|
useLayoutEffect(() => {
|
|
23
|
+
const element = unwrapRefValue(refOrElement)
|
|
24
|
+
|
|
25
25
|
let timeoutID: number | undefined = undefined
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
if (element) {
|
|
28
|
+
function debouncedHandler(event: Event) {
|
|
29
|
+
clearTimeout(timeoutID)
|
|
29
30
|
|
|
30
|
-
|
|
31
|
-
|
|
31
|
+
const delta = event.timeStamp - lastTimeStamp.current
|
|
32
|
+
scopedConsoleRef.current.logged('event:element.scroll:delta', delta)
|
|
32
33
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
callbackRef.current(event)
|
|
36
|
-
lastTimeStamp.current = event.timeStamp
|
|
37
|
-
} else {
|
|
38
|
-
timeoutID = setTimeout(() => {
|
|
39
|
-
scopedConsoleRef.current.warned('event:element.scroll:debounced', event)
|
|
40
|
-
lastTimeStamp.current = event.timeStamp
|
|
34
|
+
if (delta > interval) {
|
|
35
|
+
scopedConsoleRef.current.warned('event:element.scroll:immediately', event)
|
|
41
36
|
callbackRef.current(event)
|
|
42
|
-
|
|
37
|
+
lastTimeStamp.current = event.timeStamp
|
|
38
|
+
} else {
|
|
39
|
+
timeoutID = setTimeout(() => {
|
|
40
|
+
scopedConsoleRef.current.warned('event:element.scroll:debounced', event)
|
|
41
|
+
lastTimeStamp.current = event.timeStamp
|
|
42
|
+
callbackRef.current(event)
|
|
43
|
+
}, interval)
|
|
44
|
+
}
|
|
43
45
|
}
|
|
44
|
-
}
|
|
45
46
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
47
|
+
if (element instanceof HTMLElement) {
|
|
48
|
+
if (element instanceof HTMLBodyElement || element instanceof HTMLHtmlElement) {
|
|
49
|
+
window.addEventListener('scroll', debouncedHandler, { capture: true, passive: true })
|
|
50
|
+
window.addEventListener('resize', debouncedHandler)
|
|
50
51
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
52
|
+
return () => {
|
|
53
|
+
window.removeEventListener('scroll', debouncedHandler)
|
|
54
|
+
window.removeEventListener('resize', debouncedHandler)
|
|
55
|
+
}
|
|
56
|
+
} else {
|
|
57
|
+
element.addEventListener('scroll', debouncedHandler, { passive: true, capture: true })
|
|
57
58
|
|
|
58
|
-
|
|
59
|
-
|
|
59
|
+
return () => {
|
|
60
|
+
element.removeEventListener('scroll', debouncedHandler)
|
|
61
|
+
}
|
|
60
62
|
}
|
|
63
|
+
} else {
|
|
64
|
+
throw new Error('Exhaustive error: Expecting element to be instance of HTMLElement')
|
|
61
65
|
}
|
|
62
|
-
} else if (element) {
|
|
63
|
-
throw new Error('Exhaustive error: Expecting element to be instance of HTMLElement')
|
|
64
66
|
}
|
|
65
|
-
|
|
66
|
-
}, [element, interval, scopedConsoleRef])
|
|
67
|
+
}, [interval, refOrElement, scopedConsoleRef])
|
|
67
68
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { NestedClassName, deduplicateClassName, flatClassNameList } from '@contember/utilities'
|
|
2
2
|
import { useContext } from 'react'
|
|
3
|
+
import { useReferentiallyStableCallback } from '../referentiallyStable'
|
|
3
4
|
import { GlobalClassNamePrefixContext } from './GlobalClassNamePrefixContext'
|
|
4
5
|
|
|
5
6
|
/**
|
|
@@ -28,7 +29,7 @@ export function useClassNameFactory(
|
|
|
28
29
|
componentClassName => `${classNamePrefix}${componentClassName}`,
|
|
29
30
|
)
|
|
30
31
|
|
|
31
|
-
|
|
32
|
+
function componentClassNameFor(
|
|
32
33
|
suffix: string | null | undefined = null,
|
|
33
34
|
additionalClassName: NestedClassName = null,
|
|
34
35
|
): string {
|
|
@@ -44,4 +45,6 @@ export function useClassNameFactory(
|
|
|
44
45
|
flatClassNameList(additionalClassName),
|
|
45
46
|
)).join(' ')
|
|
46
47
|
}
|
|
48
|
+
|
|
49
|
+
return useReferentiallyStableCallback(componentClassNameFor)
|
|
47
50
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { NestedClassName, colorSchemeClassName, deduplicateClassName, filterThemedClassName, flatClassNameList } from '@contember/utilities'
|
|
2
2
|
import { useContext } from 'react'
|
|
3
|
+
import { useReferentiallyStableCallback } from '../referentiallyStable'
|
|
3
4
|
import { GlobalClassNamePrefixContext } from './GlobalClassNamePrefixContext'
|
|
4
5
|
import { useColorScheme } from './contexts'
|
|
5
6
|
|
|
@@ -17,7 +18,7 @@ export function useThemedClassNameFactory(
|
|
|
17
18
|
|
|
18
19
|
const colorScheme = colorSchemeClassName(useColorScheme())
|
|
19
20
|
|
|
20
|
-
|
|
21
|
+
function componentClassNameFor(
|
|
21
22
|
suffix: string | null | undefined = null,
|
|
22
23
|
additionalClassName: NestedClassName = null,
|
|
23
24
|
): string {
|
|
@@ -38,4 +39,6 @@ export function useThemedClassNameFactory(
|
|
|
38
39
|
),
|
|
39
40
|
)).join(' ')
|
|
40
41
|
}
|
|
42
|
+
|
|
43
|
+
return useReferentiallyStableCallback(componentClassNameFor)
|
|
41
44
|
}
|