@react-aria/disclosure 3.1.0 → 3.1.1
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.
|
@@ -48,7 +48,7 @@ function $ef53cd1bcef2de68$export$6e3e27031a30522f(props, state, ref) {
|
|
|
48
48
|
(0, $l3cx6$reactariautils.useLayoutEffect)(()=>{
|
|
49
49
|
// Cancel any pending RAF to prevent stale updates
|
|
50
50
|
if (raf.current) cancelAnimationFrame(raf.current);
|
|
51
|
-
if (ref.current && !
|
|
51
|
+
if (ref.current && !isSSR) {
|
|
52
52
|
let panel = ref.current;
|
|
53
53
|
if (isExpandedRef.current == null || typeof panel.getAnimations !== 'function') {
|
|
54
54
|
// On initial render (and in tests), set attributes without animation.
|
|
@@ -116,7 +116,7 @@ function $ef53cd1bcef2de68$export$6e3e27031a30522f(props, state, ref) {
|
|
|
116
116
|
role: 'group',
|
|
117
117
|
'aria-labelledby': triggerId,
|
|
118
118
|
'aria-hidden': !state.isExpanded,
|
|
119
|
-
hidden: isSSR
|
|
119
|
+
hidden: isSSR ? !state.isExpanded : undefined
|
|
120
120
|
}
|
|
121
121
|
};
|
|
122
122
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;;AAAA;;;;;;;;;;CAUC;;;;AAiCM,SAAS,0CAAc,KAA0B,EAAE,KAAsB,EAAE,GAAkC;IAClH,IAAI,cACF,UAAU,EACX,GAAG;IACJ,IAAI,YAAY,CAAA,GAAA,2BAAI;IACpB,IAAI,UAAU,CAAA,GAAA,2BAAI;IAClB,IAAI,QAAQ,CAAA,GAAA,4BAAO;IAEnB,IAAI,MAAM,CAAA,GAAA,mBAAK,EAAiB;IAEhC,IAAI,oBAAoB,CAAA,GAAA,wBAAU,EAAE;QAClC,+DAA+D;QAC/D,IAAI,OAAO,GAAG,sBAAsB;YAClC,IAAI,IAAI,OAAO,EACb,IAAI,OAAO,CAAC,YAAY,CAAC,UAAU;QAEvC;QACA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AY,CAAC,UAAU,gBACxC,KAAK,CAAC,KAAO;gBAClB;;YAGF,cAAc,OAAO,GAAG,MAAM,UAAU;QAC1C;IACF,GAAG;QAAC;QAAY;QAAK,MAAM,UAAU;QAAE;KAAM;IAE7C,CAAA,GAAA,sBAAQ,EAAE;QACR,OAAO;YACL,IAAI,IAAI,OAAO,EACb,qBAAqB,IAAI,OAAO;QAEpC;IACF,GAAG,EAAE;IAEL,OAAO;QACL,aAAa;YACX,IAAI;YACJ,iBAAiB,MAAM,UAAU;YACjC,iBAAiB;YACjB,SAAS,CAAC;gBACR,IAAI,CAAC,cAAc,EAAE,WAAW,KAAK,YACnC,MAAM,MAAM;YAEhB;wBACA;YACA,cAAa,CAAC;gBACZ,IAAI,EAAE,WAAW,KAAK,cAAc,CAAC,YACnC,MAAM,MAAM;YAEhB;QACF;QACA,YAAY;YACV,IAAI;YACJ,qDAAqD;YACrD,MAAM;YACN,mBAAmB;YACnB,eAAe,CAAC,MAAM,UAAU;YAChC,QAAQ,QAAQ,CAAC,MAAM,UAAU,GAAG;QACtC;IACF;AACF","sources":["packages/@react-aria/disclosure/src/useDisclosure.ts"],"sourcesContent":["/*\n * Copyright 2024 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the 'License');\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {AriaButtonProps} from '@react-types/button';\nimport {DisclosureState} from '@react-stately/disclosure';\nimport {flushSync} from 'react-dom';\nimport {HTMLAttributes, RefObject, useCallback, useEffect, useRef} from 'react';\nimport {useEvent, useId, useLayoutEffect} from '@react-aria/utils';\nimport {useIsSSR} from '@react-aria/ssr';\n\nexport interface AriaDisclosureProps {\n /** Whether the disclosure is disabled. */\n isDisabled?: boolean,\n /** Handler that is called when the disclosure's expanded state changes. */\n onExpandedChange?: (isExpanded: boolean) => void,\n /** Whether the disclosure is expanded (controlled). */\n isExpanded?: boolean,\n /** Whether the disclosure is expanded by default (uncontrolled). */\n defaultExpanded?: boolean\n}\n\nexport interface DisclosureAria {\n /** Props for the disclosure button. */\n buttonProps: AriaButtonProps,\n /** Props for the disclosure panel. */\n panelProps: HTMLAttributes<HTMLElement>\n}\n\n/**\n * Provides the behavior and accessibility implementation for a disclosure component.\n * @param props - Props for the disclosure.\n * @param state - State for the disclosure, as returned by `useDisclosureState`.\n * @param ref - A ref for the disclosure panel.\n */\nexport function useDisclosure(props: AriaDisclosureProps, state: DisclosureState, ref: RefObject<HTMLElement | null>): DisclosureAria {\n let {\n isDisabled\n } = props;\n let triggerId = useId();\n let panelId = useId();\n let isSSR = useIsSSR();\n\n let raf = useRef<number | null>(null);\n\n let handleBeforeMatch = useCallback(() => {\n // Wait a frame to revert browser's removal of hidden attribute\n raf.current = requestAnimationFrame(() => {\n if (ref.current) {\n ref.current.setAttribute('hidden', 'until-found');\n }\n });\n // Force sync state update\n flushSync(() => {\n state.toggle();\n });\n }, [ref, state]);\n\n // @ts-ignore https://github.com/facebook/react/pull/24741\n useEvent(ref, 'beforematch', handleBeforeMatch);\n\n let isExpandedRef = useRef<boolean | null>(null);\n useLayoutEffect(() => {\n // Cancel any pending RAF to prevent stale updates\n if (raf.current) {\n cancelAnimationFrame(raf.current);\n }\n if (ref.current && !isSSR) {\n let panel = ref.current;\n\n if (isExpandedRef.current == null || typeof panel.getAnimations !== 'function') {\n // On initial render (and in tests), set attributes without animation.\n if (state.isExpanded) {\n panel.removeAttribute('hidden');\n panel.style.setProperty('--disclosure-panel-width', 'auto');\n panel.style.setProperty('--disclosure-panel-height', 'auto');\n } else {\n panel.setAttribute('hidden', 'until-found');\n panel.style.setProperty('--disclosure-panel-width', '0px');\n panel.style.setProperty('--disclosure-panel-height', '0px');\n }\n } else if (state.isExpanded !== isExpandedRef.current) {\n if (state.isExpanded) {\n panel.removeAttribute('hidden');\n\n // Set the width and height as pixels so they can be animated.\n panel.style.setProperty('--disclosure-panel-width', panel.scrollWidth + 'px');\n panel.style.setProperty('--disclosure-panel-height', panel.scrollHeight + 'px');\n\n Promise.all(panel.getAnimations().map(a => a.finished))\n .then(() => {\n // After the animations complete, switch back to auto so the content can resize.\n panel.style.setProperty('--disclosure-panel-width', 'auto');\n panel.style.setProperty('--disclosure-panel-height', 'auto');\n })\n .catch(() => {});\n } else {\n panel.style.setProperty('--disclosure-panel-width', panel.scrollWidth + 'px');\n panel.style.setProperty('--disclosure-panel-height', panel.scrollHeight + 'px');\n\n // Force style re-calculation to trigger animations.\n window.getComputedStyle(panel).height;\n\n // Animate to zero size.\n panel.style.setProperty('--disclosure-panel-width', '0px');\n panel.style.setProperty('--disclosure-panel-height', '0px');\n\n // Wait for animations to apply the hidden attribute.\n Promise.all(panel.getAnimations().map(a => a.finished))\n .then(() => panel.setAttribute('hidden', 'until-found'))\n .catch(() => {});\n }\n }\n\n isExpandedRef.current = state.isExpanded;\n }\n }, [isDisabled, ref, state.isExpanded, isSSR]);\n\n useEffect(() => {\n return () => {\n if (raf.current) {\n cancelAnimationFrame(raf.current);\n }\n };\n }, []);\n\n return {\n buttonProps: {\n id: triggerId,\n 'aria-expanded': state.isExpanded,\n 'aria-controls': panelId,\n onPress: (e) => {\n if (!isDisabled && e.pointerType !== 'keyboard') {\n state.toggle();\n }\n },\n isDisabled,\n onPressStart(e) {\n if (e.pointerType === 'keyboard' && !isDisabled) {\n state.toggle();\n }\n }\n },\n panelProps: {\n id: panelId,\n // This can be overridden at the panel element level.\n role: 'group',\n 'aria-labelledby': triggerId,\n 'aria-hidden': !state.isExpanded,\n hidden: isSSR ? !state.isExpanded : undefined\n }\n };\n}\n"],"names":[],"version":3,"file":"useDisclosure.main.js.map"}
|
package/dist/useDisclosure.mjs
CHANGED
|
@@ -42,7 +42,7 @@ function $5e910fae8e128ead$export$6e3e27031a30522f(props, state, ref) {
|
|
|
42
42
|
(0, $6wN6e$useLayoutEffect)(()=>{
|
|
43
43
|
// Cancel any pending RAF to prevent stale updates
|
|
44
44
|
if (raf.current) cancelAnimationFrame(raf.current);
|
|
45
|
-
if (ref.current && !
|
|
45
|
+
if (ref.current && !isSSR) {
|
|
46
46
|
let panel = ref.current;
|
|
47
47
|
if (isExpandedRef.current == null || typeof panel.getAnimations !== 'function') {
|
|
48
48
|
// On initial render (and in tests), set attributes without animation.
|
|
@@ -110,7 +110,7 @@ function $5e910fae8e128ead$export$6e3e27031a30522f(props, state, ref) {
|
|
|
110
110
|
role: 'group',
|
|
111
111
|
'aria-labelledby': triggerId,
|
|
112
112
|
'aria-hidden': !state.isExpanded,
|
|
113
|
-
hidden: isSSR
|
|
113
|
+
hidden: isSSR ? !state.isExpanded : undefined
|
|
114
114
|
}
|
|
115
115
|
};
|
|
116
116
|
}
|
|
@@ -42,7 +42,7 @@ function $5e910fae8e128ead$export$6e3e27031a30522f(props, state, ref) {
|
|
|
42
42
|
(0, $6wN6e$useLayoutEffect)(()=>{
|
|
43
43
|
// Cancel any pending RAF to prevent stale updates
|
|
44
44
|
if (raf.current) cancelAnimationFrame(raf.current);
|
|
45
|
-
if (ref.current && !
|
|
45
|
+
if (ref.current && !isSSR) {
|
|
46
46
|
let panel = ref.current;
|
|
47
47
|
if (isExpandedRef.current == null || typeof panel.getAnimations !== 'function') {
|
|
48
48
|
// On initial render (and in tests), set attributes without animation.
|
|
@@ -110,7 +110,7 @@ function $5e910fae8e128ead$export$6e3e27031a30522f(props, state, ref) {
|
|
|
110
110
|
role: 'group',
|
|
111
111
|
'aria-labelledby': triggerId,
|
|
112
112
|
'aria-hidden': !state.isExpanded,
|
|
113
|
-
hidden: isSSR
|
|
113
|
+
hidden: isSSR ? !state.isExpanded : undefined
|
|
114
114
|
}
|
|
115
115
|
};
|
|
116
116
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;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AO;oBACL,MAAM,KAAK,CAAC,WAAW,CAAC,4BAA4B,MAAM,WAAW,GAAG;oBACxE,MAAM,KAAK,CAAC,WAAW,CAAC,6BAA6B,MAAM,YAAY,GAAG;oBAE1E,oDAAoD;oBACpD,OAAO,gBAAgB,CAAC,OAAO,MAAM;oBAErC,wBAAwB;oBACxB,MAAM,KAAK,CAAC,WAAW,CAAC,4BAA4B;oBACpD,MAAM,KAAK,CAAC,WAAW,CAAC,6BAA6B;oBAErD,qDAAqD;oBACrD,QAAQ,GAAG,CAAC,MAAM,aAAa,GAAG,GAAG,CAAC,CAAA,IAAK,EAAE,QAAQ,GAClD,IAAI,CAAC,IAAM,MAAM,YAAY,CAAC,UAAU,gBACxC,KAAK,CAAC,KAAO;gBAClB;;YAGF,cAAc,OAAO,GAAG,MAAM,UAAU;QAC1C;IACF,GAAG;QAAC;QAAY;QAAK,MAAM,UAAU;QAAE;KAAM;IAE7C,CAAA,GAAA,gBAAQ,EAAE;QACR,OAAO;YACL,IAAI,IAAI,OAAO,EACb,qBAAqB,IAAI,OAAO;QAEpC;IACF,GAAG,EAAE;IAEL,OAAO;QACL,aAAa;YACX,IAAI;YACJ,iBAAiB,MAAM,UAAU;YACjC,iBAAiB;YACjB,SAAS,CAAC;gBACR,IAAI,CAAC,cAAc,EAAE,WAAW,KAAK,YACnC,MAAM,MAAM;YAEhB;wBACA;YACA,cAAa,CAAC;gBACZ,IAAI,EAAE,WAAW,KAAK,cAAc,CAAC,YACnC,MAAM,MAAM;YAEhB;QACF;QACA,YAAY;YACV,IAAI;YACJ,qDAAqD;YACrD,MAAM;YACN,mBAAmB;YACnB,eAAe,CAAC,MAAM,UAAU;YAChC,QAAQ,QAAQ,CAAC,MAAM,UAAU,GAAG;QACtC;IACF;AACF","sources":["packages/@react-aria/disclosure/src/useDisclosure.ts"],"sourcesContent":["/*\n * Copyright 2024 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the 'License');\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {AriaButtonProps} from '@react-types/button';\nimport {DisclosureState} from '@react-stately/disclosure';\nimport {flushSync} from 'react-dom';\nimport {HTMLAttributes, RefObject, useCallback, useEffect, useRef} from 'react';\nimport {useEvent, useId, useLayoutEffect} from '@react-aria/utils';\nimport {useIsSSR} from '@react-aria/ssr';\n\nexport interface AriaDisclosureProps {\n /** Whether the disclosure is disabled. */\n isDisabled?: boolean,\n /** Handler that is called when the disclosure's expanded state changes. */\n onExpandedChange?: (isExpanded: boolean) => void,\n /** Whether the disclosure is expanded (controlled). */\n isExpanded?: boolean,\n /** Whether the disclosure is expanded by default (uncontrolled). */\n defaultExpanded?: boolean\n}\n\nexport interface DisclosureAria {\n /** Props for the disclosure button. */\n buttonProps: AriaButtonProps,\n /** Props for the disclosure panel. */\n panelProps: HTMLAttributes<HTMLElement>\n}\n\n/**\n * Provides the behavior and accessibility implementation for a disclosure component.\n * @param props - Props for the disclosure.\n * @param state - State for the disclosure, as returned by `useDisclosureState`.\n * @param ref - A ref for the disclosure panel.\n */\nexport function useDisclosure(props: AriaDisclosureProps, state: DisclosureState, ref: RefObject<HTMLElement | null>): DisclosureAria {\n let {\n isDisabled\n } = props;\n let triggerId = useId();\n let panelId = useId();\n let isSSR = useIsSSR();\n\n let raf = useRef<number | null>(null);\n\n let handleBeforeMatch = useCallback(() => {\n // Wait a frame to revert browser's removal of hidden attribute\n raf.current = requestAnimationFrame(() => {\n if (ref.current) {\n ref.current.setAttribute('hidden', 'until-found');\n }\n });\n // Force sync state update\n flushSync(() => {\n state.toggle();\n });\n }, [ref, state]);\n\n // @ts-ignore https://github.com/facebook/react/pull/24741\n useEvent(ref, 'beforematch', handleBeforeMatch);\n\n let isExpandedRef = useRef<boolean | null>(null);\n useLayoutEffect(() => {\n // Cancel any pending RAF to prevent stale updates\n if (raf.current) {\n cancelAnimationFrame(raf.current);\n }\n if (ref.current && !isSSR) {\n let panel = ref.current;\n\n if (isExpandedRef.current == null || typeof panel.getAnimations !== 'function') {\n // On initial render (and in tests), set attributes without animation.\n if (state.isExpanded) {\n panel.removeAttribute('hidden');\n panel.style.setProperty('--disclosure-panel-width', 'auto');\n panel.style.setProperty('--disclosure-panel-height', 'auto');\n } else {\n panel.setAttribute('hidden', 'until-found');\n panel.style.setProperty('--disclosure-panel-width', '0px');\n panel.style.setProperty('--disclosure-panel-height', '0px');\n }\n } else if (state.isExpanded !== isExpandedRef.current) {\n if (state.isExpanded) {\n panel.removeAttribute('hidden');\n\n // Set the width and height as pixels so they can be animated.\n panel.style.setProperty('--disclosure-panel-width', panel.scrollWidth + 'px');\n panel.style.setProperty('--disclosure-panel-height', panel.scrollHeight + 'px');\n\n Promise.all(panel.getAnimations().map(a => a.finished))\n .then(() => {\n // After the animations complete, switch back to auto so the content can resize.\n panel.style.setProperty('--disclosure-panel-width', 'auto');\n panel.style.setProperty('--disclosure-panel-height', 'auto');\n })\n .catch(() => {});\n } else {\n panel.style.setProperty('--disclosure-panel-width', panel.scrollWidth + 'px');\n panel.style.setProperty('--disclosure-panel-height', panel.scrollHeight + 'px');\n\n // Force style re-calculation to trigger animations.\n window.getComputedStyle(panel).height;\n\n // Animate to zero size.\n panel.style.setProperty('--disclosure-panel-width', '0px');\n panel.style.setProperty('--disclosure-panel-height', '0px');\n\n // Wait for animations to apply the hidden attribute.\n Promise.all(panel.getAnimations().map(a => a.finished))\n .then(() => panel.setAttribute('hidden', 'until-found'))\n .catch(() => {});\n }\n }\n\n isExpandedRef.current = state.isExpanded;\n }\n }, [isDisabled, ref, state.isExpanded, isSSR]);\n\n useEffect(() => {\n return () => {\n if (raf.current) {\n cancelAnimationFrame(raf.current);\n }\n };\n }, []);\n\n return {\n buttonProps: {\n id: triggerId,\n 'aria-expanded': state.isExpanded,\n 'aria-controls': panelId,\n onPress: (e) => {\n if (!isDisabled && e.pointerType !== 'keyboard') {\n state.toggle();\n }\n },\n isDisabled,\n onPressStart(e) {\n if (e.pointerType === 'keyboard' && !isDisabled) {\n state.toggle();\n }\n }\n },\n panelProps: {\n id: panelId,\n // This can be overridden at the panel element level.\n role: 'group',\n 'aria-labelledby': triggerId,\n 'aria-hidden': !state.isExpanded,\n hidden: isSSR ? !state.isExpanded : undefined\n }\n };\n}\n"],"names":[],"version":3,"file":"useDisclosure.module.js.map"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-aria/disclosure",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.1",
|
|
4
4
|
"description": "Spectrum UI components in React",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@react-aria/ssr": "^3.9.10",
|
|
30
|
-
"@react-aria/utils": "^3.
|
|
31
|
-
"@react-stately/disclosure": "^3.0.
|
|
30
|
+
"@react-aria/utils": "^3.32.0",
|
|
31
|
+
"@react-stately/disclosure": "^3.0.9",
|
|
32
32
|
"@react-types/button": "^3.14.1",
|
|
33
33
|
"@swc/helpers": "^0.5.0"
|
|
34
34
|
},
|
|
@@ -39,5 +39,5 @@
|
|
|
39
39
|
"publishConfig": {
|
|
40
40
|
"access": "public"
|
|
41
41
|
},
|
|
42
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "4d838da5bfe36abb35aed166995a9ef63825370f"
|
|
43
43
|
}
|
package/src/useDisclosure.ts
CHANGED
|
@@ -73,7 +73,7 @@ export function useDisclosure(props: AriaDisclosureProps, state: DisclosureState
|
|
|
73
73
|
if (raf.current) {
|
|
74
74
|
cancelAnimationFrame(raf.current);
|
|
75
75
|
}
|
|
76
|
-
if (ref.current && !
|
|
76
|
+
if (ref.current && !isSSR) {
|
|
77
77
|
let panel = ref.current;
|
|
78
78
|
|
|
79
79
|
if (isExpandedRef.current == null || typeof panel.getAnimations !== 'function') {
|
|
@@ -155,7 +155,7 @@ export function useDisclosure(props: AriaDisclosureProps, state: DisclosureState
|
|
|
155
155
|
role: 'group',
|
|
156
156
|
'aria-labelledby': triggerId,
|
|
157
157
|
'aria-hidden': !state.isExpanded,
|
|
158
|
-
hidden:
|
|
158
|
+
hidden: isSSR ? !state.isExpanded : undefined
|
|
159
159
|
}
|
|
160
160
|
};
|
|
161
161
|
}
|