@react-aria/ssr 3.0.0-nightly-641446f65-240905
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/README.md +3 -0
- package/dist/SSRProvider.main.js +155 -0
- package/dist/SSRProvider.main.js.map +1 -0
- package/dist/SSRProvider.mjs +144 -0
- package/dist/SSRProvider.module.js +144 -0
- package/dist/SSRProvider.module.js.map +1 -0
- package/dist/import.mjs +17 -0
- package/dist/main.js +24 -0
- package/dist/main.js.map +1 -0
- package/dist/module.js +17 -0
- package/dist/module.js.map +1 -0
- package/dist/types.d.ts +21 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +45 -0
- package/src/SSRProvider.tsx +198 -0
- package/src/index.ts +13 -0
package/README.md
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
var $14Xyt$react = require("react");
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
function $parcel$interopDefault(a) {
|
|
5
|
+
return a && a.__esModule ? a.default : a;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function $parcel$export(e, n, v, s) {
|
|
9
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
$parcel$export(module.exports, "SSRProvider", () => $97d95f6660b1bb14$export$9f8ac96af4b1b2ae);
|
|
13
|
+
$parcel$export(module.exports, "useIsSSR", () => $97d95f6660b1bb14$export$535bd6ca7f90a273);
|
|
14
|
+
$parcel$export(module.exports, "useSSRSafeId", () => $97d95f6660b1bb14$export$619500959fc48b26);
|
|
15
|
+
/*
|
|
16
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
17
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
18
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
19
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
20
|
+
*
|
|
21
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
22
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
23
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
24
|
+
* governing permissions and limitations under the License.
|
|
25
|
+
*/ // We must avoid a circular dependency with @react-aria/utils, and this useLayoutEffect is
|
|
26
|
+
// guarded by a check that it only runs on the client side.
|
|
27
|
+
// eslint-disable-next-line rulesdir/useLayoutEffectRule
|
|
28
|
+
|
|
29
|
+
// Default context value to use in case there is no SSRProvider. This is fine for
|
|
30
|
+
// client-only apps. In order to support multiple copies of React Aria potentially
|
|
31
|
+
// being on the page at once, the prefix is set to a random number. SSRProvider
|
|
32
|
+
// will reset this to zero for consistency between server and client, so in the
|
|
33
|
+
// SSR case multiple copies of React Aria is not supported.
|
|
34
|
+
const $97d95f6660b1bb14$var$defaultContext = {
|
|
35
|
+
prefix: String(Math.round(Math.random() * 10000000000)),
|
|
36
|
+
current: 0
|
|
37
|
+
};
|
|
38
|
+
const $97d95f6660b1bb14$var$SSRContext = /*#__PURE__*/ (0, ($parcel$interopDefault($14Xyt$react))).createContext($97d95f6660b1bb14$var$defaultContext);
|
|
39
|
+
const $97d95f6660b1bb14$var$IsSSRContext = /*#__PURE__*/ (0, ($parcel$interopDefault($14Xyt$react))).createContext(false);
|
|
40
|
+
// This is only used in React < 18.
|
|
41
|
+
function $97d95f6660b1bb14$var$LegacySSRProvider(props) {
|
|
42
|
+
let cur = (0, $14Xyt$react.useContext)($97d95f6660b1bb14$var$SSRContext);
|
|
43
|
+
let counter = $97d95f6660b1bb14$var$useCounter(cur === $97d95f6660b1bb14$var$defaultContext);
|
|
44
|
+
let [isSSR, setIsSSR] = (0, $14Xyt$react.useState)(true);
|
|
45
|
+
let value = (0, $14Xyt$react.useMemo)(()=>({
|
|
46
|
+
// If this is the first SSRProvider, start with an empty string prefix, otherwise
|
|
47
|
+
// append and increment the counter.
|
|
48
|
+
prefix: cur === $97d95f6660b1bb14$var$defaultContext ? '' : `${cur.prefix}-${counter}`,
|
|
49
|
+
current: 0
|
|
50
|
+
}), [
|
|
51
|
+
cur,
|
|
52
|
+
counter
|
|
53
|
+
]);
|
|
54
|
+
// If on the client, and the component was initially server rendered,
|
|
55
|
+
// then schedule a layout effect to update the component after hydration.
|
|
56
|
+
if (typeof document !== 'undefined') // This if statement technically breaks the rules of hooks, but is safe
|
|
57
|
+
// because the condition never changes after mounting.
|
|
58
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
59
|
+
(0, $14Xyt$react.useLayoutEffect)(()=>{
|
|
60
|
+
setIsSSR(false);
|
|
61
|
+
}, []);
|
|
62
|
+
return /*#__PURE__*/ (0, ($parcel$interopDefault($14Xyt$react))).createElement($97d95f6660b1bb14$var$SSRContext.Provider, {
|
|
63
|
+
value: value
|
|
64
|
+
}, /*#__PURE__*/ (0, ($parcel$interopDefault($14Xyt$react))).createElement($97d95f6660b1bb14$var$IsSSRContext.Provider, {
|
|
65
|
+
value: isSSR
|
|
66
|
+
}, props.children));
|
|
67
|
+
}
|
|
68
|
+
let $97d95f6660b1bb14$var$warnedAboutSSRProvider = false;
|
|
69
|
+
function $97d95f6660b1bb14$export$9f8ac96af4b1b2ae(props) {
|
|
70
|
+
if (typeof (0, ($parcel$interopDefault($14Xyt$react)))['useId'] === 'function') {
|
|
71
|
+
if (process.env.NODE_ENV !== 'test' && !$97d95f6660b1bb14$var$warnedAboutSSRProvider) {
|
|
72
|
+
console.warn('In React 18, SSRProvider is not necessary and is a noop. You can remove it from your app.');
|
|
73
|
+
$97d95f6660b1bb14$var$warnedAboutSSRProvider = true;
|
|
74
|
+
}
|
|
75
|
+
return /*#__PURE__*/ (0, ($parcel$interopDefault($14Xyt$react))).createElement((0, ($parcel$interopDefault($14Xyt$react))).Fragment, null, props.children);
|
|
76
|
+
}
|
|
77
|
+
return /*#__PURE__*/ (0, ($parcel$interopDefault($14Xyt$react))).createElement($97d95f6660b1bb14$var$LegacySSRProvider, props);
|
|
78
|
+
}
|
|
79
|
+
let $97d95f6660b1bb14$var$canUseDOM = Boolean(typeof window !== 'undefined' && window.document && window.document.createElement);
|
|
80
|
+
let $97d95f6660b1bb14$var$componentIds = new WeakMap();
|
|
81
|
+
function $97d95f6660b1bb14$var$useCounter(isDisabled = false) {
|
|
82
|
+
let ctx = (0, $14Xyt$react.useContext)($97d95f6660b1bb14$var$SSRContext);
|
|
83
|
+
let ref = (0, $14Xyt$react.useRef)(null);
|
|
84
|
+
// eslint-disable-next-line rulesdir/pure-render
|
|
85
|
+
if (ref.current === null && !isDisabled) {
|
|
86
|
+
var _React___SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED_ReactCurrentOwner, _React___SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
|
|
87
|
+
// In strict mode, React renders components twice, and the ref will be reset to null on the second render.
|
|
88
|
+
// This means our id counter will be incremented twice instead of once. This is a problem because on the
|
|
89
|
+
// server, components are only rendered once and so ids generated on the server won't match the client.
|
|
90
|
+
// In React 18, useId was introduced to solve this, but it is not available in older versions. So to solve this
|
|
91
|
+
// we need to use some React internals to access the underlying Fiber instance, which is stable between renders.
|
|
92
|
+
// This is exposed as ReactCurrentOwner in development, which is all we need since StrictMode only runs in development.
|
|
93
|
+
// To ensure that we only increment the global counter once, we store the starting id for this component in
|
|
94
|
+
// a weak map associated with the Fiber. On the second render, we reset the global counter to this value.
|
|
95
|
+
// Since React runs the second render immediately after the first, this is safe.
|
|
96
|
+
// @ts-ignore
|
|
97
|
+
let currentOwner = (_React___SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED = (0, ($parcel$interopDefault($14Xyt$react))).__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED) === null || _React___SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED === void 0 ? void 0 : (_React___SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED_ReactCurrentOwner = _React___SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner) === null || _React___SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED_ReactCurrentOwner === void 0 ? void 0 : _React___SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED_ReactCurrentOwner.current;
|
|
98
|
+
if (currentOwner) {
|
|
99
|
+
let prevComponentValue = $97d95f6660b1bb14$var$componentIds.get(currentOwner);
|
|
100
|
+
if (prevComponentValue == null) // On the first render, and first call to useId, store the id and state in our weak map.
|
|
101
|
+
$97d95f6660b1bb14$var$componentIds.set(currentOwner, {
|
|
102
|
+
id: ctx.current,
|
|
103
|
+
state: currentOwner.memoizedState
|
|
104
|
+
});
|
|
105
|
+
else if (currentOwner.memoizedState !== prevComponentValue.state) {
|
|
106
|
+
// On the second render, the memoizedState gets reset by React.
|
|
107
|
+
// Reset the counter, and remove from the weak map so we don't
|
|
108
|
+
// do this for subsequent useId calls.
|
|
109
|
+
ctx.current = prevComponentValue.id;
|
|
110
|
+
$97d95f6660b1bb14$var$componentIds.delete(currentOwner);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// eslint-disable-next-line rulesdir/pure-render
|
|
114
|
+
ref.current = ++ctx.current;
|
|
115
|
+
}
|
|
116
|
+
// eslint-disable-next-line rulesdir/pure-render
|
|
117
|
+
return ref.current;
|
|
118
|
+
}
|
|
119
|
+
function $97d95f6660b1bb14$var$useLegacySSRSafeId(defaultId) {
|
|
120
|
+
let ctx = (0, $14Xyt$react.useContext)($97d95f6660b1bb14$var$SSRContext);
|
|
121
|
+
// If we are rendering in a non-DOM environment, and there's no SSRProvider,
|
|
122
|
+
// provide a warning to hint to the developer to add one.
|
|
123
|
+
if (ctx === $97d95f6660b1bb14$var$defaultContext && !$97d95f6660b1bb14$var$canUseDOM) console.warn('When server rendering, you must wrap your application in an <SSRProvider> to ensure consistent ids are generated between the client and server.');
|
|
124
|
+
let counter = $97d95f6660b1bb14$var$useCounter(!!defaultId);
|
|
125
|
+
let prefix = ctx === $97d95f6660b1bb14$var$defaultContext && process.env.NODE_ENV === 'test' ? 'react-aria' : `react-aria${ctx.prefix}`;
|
|
126
|
+
return defaultId || `${prefix}-${counter}`;
|
|
127
|
+
}
|
|
128
|
+
function $97d95f6660b1bb14$var$useModernSSRSafeId(defaultId) {
|
|
129
|
+
// @ts-ignore
|
|
130
|
+
let id = (0, ($parcel$interopDefault($14Xyt$react))).useId();
|
|
131
|
+
let [didSSR] = (0, $14Xyt$react.useState)($97d95f6660b1bb14$export$535bd6ca7f90a273());
|
|
132
|
+
let prefix = didSSR || process.env.NODE_ENV === 'test' ? 'react-aria' : `react-aria${$97d95f6660b1bb14$var$defaultContext.prefix}`;
|
|
133
|
+
return defaultId || `${prefix}-${id}`;
|
|
134
|
+
}
|
|
135
|
+
const $97d95f6660b1bb14$export$619500959fc48b26 = typeof (0, ($parcel$interopDefault($14Xyt$react)))['useId'] === 'function' ? $97d95f6660b1bb14$var$useModernSSRSafeId : $97d95f6660b1bb14$var$useLegacySSRSafeId;
|
|
136
|
+
function $97d95f6660b1bb14$var$getSnapshot() {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
function $97d95f6660b1bb14$var$getServerSnapshot() {
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
143
|
+
function $97d95f6660b1bb14$var$subscribe(onStoreChange) {
|
|
144
|
+
// noop
|
|
145
|
+
return ()=>{};
|
|
146
|
+
}
|
|
147
|
+
function $97d95f6660b1bb14$export$535bd6ca7f90a273() {
|
|
148
|
+
// In React 18, we can use useSyncExternalStore to detect if we're server rendering or hydrating.
|
|
149
|
+
if (typeof (0, ($parcel$interopDefault($14Xyt$react)))['useSyncExternalStore'] === 'function') return (0, ($parcel$interopDefault($14Xyt$react)))['useSyncExternalStore']($97d95f6660b1bb14$var$subscribe, $97d95f6660b1bb14$var$getSnapshot, $97d95f6660b1bb14$var$getServerSnapshot);
|
|
150
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
151
|
+
return (0, $14Xyt$react.useContext)($97d95f6660b1bb14$var$IsSSRContext);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
//# sourceMappingURL=SSRProvider.main.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;;;;;;;;;;;;;AAAA;;;;;;;;;;CAUC,GAED,0FAA0F;AAC1F,2DAA2D;AAC3D,wDAAwD;;AAcxD,iFAAiF;AACjF,kFAAkF;AAClF,+EAA+E;AAC/E,+EAA+E;AAC/E,2DAA2D;AAC3D,MAAM,uCAAkC;IACtC,QAAQ,OAAO,KAAK,KAAK,CAAC,KAAK,MAAM,KAAK;IAC1C,SAAS;AACX;AAEA,MAAM,iDAAa,CAAA,GAAA,sCAAI,EAAE,aAAa,CAAkB;AACxD,MAAM,mDAAe,CAAA,GAAA,sCAAI,EAAE,aAAa,CAAC;AAOzC,mCAAmC;AACnC,SAAS,wCAAkB,KAAuB;IAChD,IAAI,MAAM,CAAA,GAAA,uBAAS,EAAE;IACrB,IAAI,UAAU,iCAAW,QAAQ;IACjC,IAAI,CAAC,OAAO,SAAS,GAAG,CAAA,GAAA,qBAAO,EAAE;IACjC,IAAI,QAAyB,CAAA,GAAA,oBAAM,EAAE,IAAO,CAAA;YAC1C,iFAAiF;YACjF,oCAAoC;YACpC,QAAQ,QAAQ,uCAAiB,KAAK,CAAC,EAAE,IAAI,MAAM,CAAC,CAAC,EAAE,QAAQ,CAAC;YAChE,SAAS;QACX,CAAA,GAAI;QAAC;QAAK;KAAQ;IAElB,qEAAqE;IACrE,yEAAyE;IACzE,IAAI,OAAO,aAAa,aACtB,uEAAuE;IACvE,sDAAsD;IACtD,sDAAsD;IACtD,CAAA,GAAA,4BAAc,EAAE;QACd,SAAS;IACX,GAAG,EAAE;IAGP,qBACE,0DAAC,iCAAW,QAAQ;QAAC,O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AA,GAAA,sCAAI,CAAC,CAAC,uBAAuB,CAAC,iCAAW,mCAAa;IAG/D,sDAAsD;IACtD,OAAO,CAAA,GAAA,uBAAS,EAAE;AACpB","sources":["packages/@react-aria/ssr/src/SSRProvider.tsx"],"sourcesContent":["/*\n * Copyright 2020 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\n// We must avoid a circular dependency with @react-aria/utils, and this useLayoutEffect is\n// guarded by a check that it only runs on the client side.\n// eslint-disable-next-line rulesdir/useLayoutEffectRule\nimport React, {JSX, ReactNode, useContext, useLayoutEffect, useMemo, useRef, useState} from 'react';\n\n// To support SSR, the auto incrementing id counter is stored in a context. This allows\n// it to be reset on every request to ensure the client and server are consistent.\n// There is also a prefix string that is used to support async loading components\n// Each async boundary must be wrapped in an SSR provider, which appends to the prefix\n// and resets the current id counter. This ensures that async loaded components have\n// consistent ids regardless of the loading order.\ninterface SSRContextValue {\n prefix: string,\n current: number\n}\n\n// Default context value to use in case there is no SSRProvider. This is fine for\n// client-only apps. In order to support multiple copies of React Aria potentially\n// being on the page at once, the prefix is set to a random number. SSRProvider\n// will reset this to zero for consistency between server and client, so in the\n// SSR case multiple copies of React Aria is not supported.\nconst defaultContext: SSRContextValue = {\n prefix: String(Math.round(Math.random() * 10000000000)),\n current: 0\n};\n\nconst SSRContext = React.createContext<SSRContextValue>(defaultContext);\nconst IsSSRContext = React.createContext(false);\n\nexport interface SSRProviderProps {\n /** Your application here. */\n children: ReactNode\n}\n\n// This is only used in React < 18.\nfunction LegacySSRProvider(props: SSRProviderProps): JSX.Element {\n let cur = useContext(SSRContext);\n let counter = useCounter(cur === defaultContext);\n let [isSSR, setIsSSR] = useState(true);\n let value: SSRContextValue = useMemo(() => ({\n // If this is the first SSRProvider, start with an empty string prefix, otherwise\n // append and increment the counter.\n prefix: cur === defaultContext ? '' : `${cur.prefix}-${counter}`,\n current: 0\n }), [cur, counter]);\n\n // If on the client, and the component was initially server rendered,\n // then schedule a layout effect to update the component after hydration.\n if (typeof document !== 'undefined') {\n // This if statement technically breaks the rules of hooks, but is safe\n // because the condition never changes after mounting.\n // eslint-disable-next-line react-hooks/rules-of-hooks\n useLayoutEffect(() => {\n setIsSSR(false);\n }, []);\n }\n\n return (\n <SSRContext.Provider value={value}>\n <IsSSRContext.Provider value={isSSR}>\n {props.children}\n </IsSSRContext.Provider>\n </SSRContext.Provider>\n );\n}\n\nlet warnedAboutSSRProvider = false;\n\n/**\n * When using SSR with React Aria in React 16 or 17, applications must be wrapped in an SSRProvider.\n * This ensures that auto generated ids are consistent between the client and server.\n */\nexport function SSRProvider(props: SSRProviderProps): JSX.Element {\n if (typeof React['useId'] === 'function') {\n if (process.env.NODE_ENV !== 'test' && !warnedAboutSSRProvider) {\n console.warn('In React 18, SSRProvider is not necessary and is a noop. You can remove it from your app.');\n warnedAboutSSRProvider = true;\n }\n return <>{props.children}</>;\n }\n return <LegacySSRProvider {...props} />;\n}\n\nlet canUseDOM = Boolean(\n typeof window !== 'undefined' &&\n window.document &&\n window.document.createElement\n);\n\nlet componentIds = new WeakMap();\n\nfunction useCounter(isDisabled = false) {\n let ctx = useContext(SSRContext);\n let ref = useRef<number | null>(null);\n // eslint-disable-next-line rulesdir/pure-render\n if (ref.current === null && !isDisabled) {\n // In strict mode, React renders components twice, and the ref will be reset to null on the second render.\n // This means our id counter will be incremented twice instead of once. This is a problem because on the\n // server, components are only rendered once and so ids generated on the server won't match the client.\n // In React 18, useId was introduced to solve this, but it is not available in older versions. So to solve this\n // we need to use some React internals to access the underlying Fiber instance, which is stable between renders.\n // This is exposed as ReactCurrentOwner in development, which is all we need since StrictMode only runs in development.\n // To ensure that we only increment the global counter once, we store the starting id for this component in\n // a weak map associated with the Fiber. On the second render, we reset the global counter to this value.\n // Since React runs the second render immediately after the first, this is safe.\n // @ts-ignore\n let currentOwner = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED?.ReactCurrentOwner?.current;\n if (currentOwner) {\n let prevComponentValue = componentIds.get(currentOwner);\n if (prevComponentValue == null) {\n // On the first render, and first call to useId, store the id and state in our weak map.\n componentIds.set(currentOwner, {\n id: ctx.current,\n state: currentOwner.memoizedState\n });\n } else if (currentOwner.memoizedState !== prevComponentValue.state) {\n // On the second render, the memoizedState gets reset by React.\n // Reset the counter, and remove from the weak map so we don't\n // do this for subsequent useId calls.\n ctx.current = prevComponentValue.id;\n componentIds.delete(currentOwner);\n }\n }\n\n // eslint-disable-next-line rulesdir/pure-render\n ref.current = ++ctx.current;\n }\n\n // eslint-disable-next-line rulesdir/pure-render\n return ref.current;\n}\n\nfunction useLegacySSRSafeId(defaultId?: string): string {\n let ctx = useContext(SSRContext);\n\n // If we are rendering in a non-DOM environment, and there's no SSRProvider,\n // provide a warning to hint to the developer to add one.\n if (ctx === defaultContext && !canUseDOM) {\n console.warn('When server rendering, you must wrap your application in an <SSRProvider> to ensure consistent ids are generated between the client and server.');\n }\n\n let counter = useCounter(!!defaultId);\n let prefix = ctx === defaultContext && process.env.NODE_ENV === 'test' ? 'react-aria' : `react-aria${ctx.prefix}`;\n return defaultId || `${prefix}-${counter}`;\n}\n\nfunction useModernSSRSafeId(defaultId?: string): string {\n // @ts-ignore\n let id = React.useId();\n let [didSSR] = useState(useIsSSR());\n let prefix = didSSR || process.env.NODE_ENV === 'test' ? 'react-aria' : `react-aria${defaultContext.prefix}`;\n return defaultId || `${prefix}-${id}`;\n}\n\n// Use React.useId in React 18 if available, otherwise fall back to our old implementation.\n/** @private */\nexport const useSSRSafeId = typeof React['useId'] === 'function' ? useModernSSRSafeId : useLegacySSRSafeId;\n\nfunction getSnapshot() {\n return false;\n}\n\nfunction getServerSnapshot() {\n return true;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nfunction subscribe(onStoreChange: () => void): () => void {\n // noop\n return () => {};\n}\n\n/**\n * Returns whether the component is currently being server side rendered or\n * hydrated on the client. Can be used to delay browser-specific rendering\n * until after hydration.\n */\nexport function useIsSSR(): boolean {\n // In React 18, we can use useSyncExternalStore to detect if we're server rendering or hydrating.\n if (typeof React['useSyncExternalStore'] === 'function') {\n return React['useSyncExternalStore'](subscribe, getSnapshot, getServerSnapshot);\n }\n\n // eslint-disable-next-line react-hooks/rules-of-hooks\n return useContext(IsSSRContext);\n}\n"],"names":[],"version":3,"file":"SSRProvider.main.js.map","sourceRoot":"../../../../"}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import $670gB$react, {useContext as $670gB$useContext, useState as $670gB$useState, useMemo as $670gB$useMemo, useLayoutEffect as $670gB$useLayoutEffect, useRef as $670gB$useRef} from "react";
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
+
* governing permissions and limitations under the License.
|
|
13
|
+
*/ // We must avoid a circular dependency with @react-aria/utils, and this useLayoutEffect is
|
|
14
|
+
// guarded by a check that it only runs on the client side.
|
|
15
|
+
// eslint-disable-next-line rulesdir/useLayoutEffectRule
|
|
16
|
+
|
|
17
|
+
// Default context value to use in case there is no SSRProvider. This is fine for
|
|
18
|
+
// client-only apps. In order to support multiple copies of React Aria potentially
|
|
19
|
+
// being on the page at once, the prefix is set to a random number. SSRProvider
|
|
20
|
+
// will reset this to zero for consistency between server and client, so in the
|
|
21
|
+
// SSR case multiple copies of React Aria is not supported.
|
|
22
|
+
const $b5e257d569688ac6$var$defaultContext = {
|
|
23
|
+
prefix: String(Math.round(Math.random() * 10000000000)),
|
|
24
|
+
current: 0
|
|
25
|
+
};
|
|
26
|
+
const $b5e257d569688ac6$var$SSRContext = /*#__PURE__*/ (0, $670gB$react).createContext($b5e257d569688ac6$var$defaultContext);
|
|
27
|
+
const $b5e257d569688ac6$var$IsSSRContext = /*#__PURE__*/ (0, $670gB$react).createContext(false);
|
|
28
|
+
// This is only used in React < 18.
|
|
29
|
+
function $b5e257d569688ac6$var$LegacySSRProvider(props) {
|
|
30
|
+
let cur = (0, $670gB$useContext)($b5e257d569688ac6$var$SSRContext);
|
|
31
|
+
let counter = $b5e257d569688ac6$var$useCounter(cur === $b5e257d569688ac6$var$defaultContext);
|
|
32
|
+
let [isSSR, setIsSSR] = (0, $670gB$useState)(true);
|
|
33
|
+
let value = (0, $670gB$useMemo)(()=>({
|
|
34
|
+
// If this is the first SSRProvider, start with an empty string prefix, otherwise
|
|
35
|
+
// append and increment the counter.
|
|
36
|
+
prefix: cur === $b5e257d569688ac6$var$defaultContext ? '' : `${cur.prefix}-${counter}`,
|
|
37
|
+
current: 0
|
|
38
|
+
}), [
|
|
39
|
+
cur,
|
|
40
|
+
counter
|
|
41
|
+
]);
|
|
42
|
+
// If on the client, and the component was initially server rendered,
|
|
43
|
+
// then schedule a layout effect to update the component after hydration.
|
|
44
|
+
if (typeof document !== 'undefined') // This if statement technically breaks the rules of hooks, but is safe
|
|
45
|
+
// because the condition never changes after mounting.
|
|
46
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
47
|
+
(0, $670gB$useLayoutEffect)(()=>{
|
|
48
|
+
setIsSSR(false);
|
|
49
|
+
}, []);
|
|
50
|
+
return /*#__PURE__*/ (0, $670gB$react).createElement($b5e257d569688ac6$var$SSRContext.Provider, {
|
|
51
|
+
value: value
|
|
52
|
+
}, /*#__PURE__*/ (0, $670gB$react).createElement($b5e257d569688ac6$var$IsSSRContext.Provider, {
|
|
53
|
+
value: isSSR
|
|
54
|
+
}, props.children));
|
|
55
|
+
}
|
|
56
|
+
let $b5e257d569688ac6$var$warnedAboutSSRProvider = false;
|
|
57
|
+
function $b5e257d569688ac6$export$9f8ac96af4b1b2ae(props) {
|
|
58
|
+
if (typeof (0, $670gB$react)['useId'] === 'function') {
|
|
59
|
+
if (process.env.NODE_ENV !== 'test' && !$b5e257d569688ac6$var$warnedAboutSSRProvider) {
|
|
60
|
+
console.warn('In React 18, SSRProvider is not necessary and is a noop. You can remove it from your app.');
|
|
61
|
+
$b5e257d569688ac6$var$warnedAboutSSRProvider = true;
|
|
62
|
+
}
|
|
63
|
+
return /*#__PURE__*/ (0, $670gB$react).createElement((0, $670gB$react).Fragment, null, props.children);
|
|
64
|
+
}
|
|
65
|
+
return /*#__PURE__*/ (0, $670gB$react).createElement($b5e257d569688ac6$var$LegacySSRProvider, props);
|
|
66
|
+
}
|
|
67
|
+
let $b5e257d569688ac6$var$canUseDOM = Boolean(typeof window !== 'undefined' && window.document && window.document.createElement);
|
|
68
|
+
let $b5e257d569688ac6$var$componentIds = new WeakMap();
|
|
69
|
+
function $b5e257d569688ac6$var$useCounter(isDisabled = false) {
|
|
70
|
+
let ctx = (0, $670gB$useContext)($b5e257d569688ac6$var$SSRContext);
|
|
71
|
+
let ref = (0, $670gB$useRef)(null);
|
|
72
|
+
// eslint-disable-next-line rulesdir/pure-render
|
|
73
|
+
if (ref.current === null && !isDisabled) {
|
|
74
|
+
var _React___SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED_ReactCurrentOwner, _React___SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
|
|
75
|
+
// In strict mode, React renders components twice, and the ref will be reset to null on the second render.
|
|
76
|
+
// This means our id counter will be incremented twice instead of once. This is a problem because on the
|
|
77
|
+
// server, components are only rendered once and so ids generated on the server won't match the client.
|
|
78
|
+
// In React 18, useId was introduced to solve this, but it is not available in older versions. So to solve this
|
|
79
|
+
// we need to use some React internals to access the underlying Fiber instance, which is stable between renders.
|
|
80
|
+
// This is exposed as ReactCurrentOwner in development, which is all we need since StrictMode only runs in development.
|
|
81
|
+
// To ensure that we only increment the global counter once, we store the starting id for this component in
|
|
82
|
+
// a weak map associated with the Fiber. On the second render, we reset the global counter to this value.
|
|
83
|
+
// Since React runs the second render immediately after the first, this is safe.
|
|
84
|
+
// @ts-ignore
|
|
85
|
+
let currentOwner = (_React___SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED = (0, $670gB$react).__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED) === null || _React___SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED === void 0 ? void 0 : (_React___SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED_ReactCurrentOwner = _React___SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner) === null || _React___SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED_ReactCurrentOwner === void 0 ? void 0 : _React___SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED_ReactCurrentOwner.current;
|
|
86
|
+
if (currentOwner) {
|
|
87
|
+
let prevComponentValue = $b5e257d569688ac6$var$componentIds.get(currentOwner);
|
|
88
|
+
if (prevComponentValue == null) // On the first render, and first call to useId, store the id and state in our weak map.
|
|
89
|
+
$b5e257d569688ac6$var$componentIds.set(currentOwner, {
|
|
90
|
+
id: ctx.current,
|
|
91
|
+
state: currentOwner.memoizedState
|
|
92
|
+
});
|
|
93
|
+
else if (currentOwner.memoizedState !== prevComponentValue.state) {
|
|
94
|
+
// On the second render, the memoizedState gets reset by React.
|
|
95
|
+
// Reset the counter, and remove from the weak map so we don't
|
|
96
|
+
// do this for subsequent useId calls.
|
|
97
|
+
ctx.current = prevComponentValue.id;
|
|
98
|
+
$b5e257d569688ac6$var$componentIds.delete(currentOwner);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
// eslint-disable-next-line rulesdir/pure-render
|
|
102
|
+
ref.current = ++ctx.current;
|
|
103
|
+
}
|
|
104
|
+
// eslint-disable-next-line rulesdir/pure-render
|
|
105
|
+
return ref.current;
|
|
106
|
+
}
|
|
107
|
+
function $b5e257d569688ac6$var$useLegacySSRSafeId(defaultId) {
|
|
108
|
+
let ctx = (0, $670gB$useContext)($b5e257d569688ac6$var$SSRContext);
|
|
109
|
+
// If we are rendering in a non-DOM environment, and there's no SSRProvider,
|
|
110
|
+
// provide a warning to hint to the developer to add one.
|
|
111
|
+
if (ctx === $b5e257d569688ac6$var$defaultContext && !$b5e257d569688ac6$var$canUseDOM) console.warn('When server rendering, you must wrap your application in an <SSRProvider> to ensure consistent ids are generated between the client and server.');
|
|
112
|
+
let counter = $b5e257d569688ac6$var$useCounter(!!defaultId);
|
|
113
|
+
let prefix = ctx === $b5e257d569688ac6$var$defaultContext && process.env.NODE_ENV === 'test' ? 'react-aria' : `react-aria${ctx.prefix}`;
|
|
114
|
+
return defaultId || `${prefix}-${counter}`;
|
|
115
|
+
}
|
|
116
|
+
function $b5e257d569688ac6$var$useModernSSRSafeId(defaultId) {
|
|
117
|
+
// @ts-ignore
|
|
118
|
+
let id = (0, $670gB$react).useId();
|
|
119
|
+
let [didSSR] = (0, $670gB$useState)($b5e257d569688ac6$export$535bd6ca7f90a273());
|
|
120
|
+
let prefix = didSSR || process.env.NODE_ENV === 'test' ? 'react-aria' : `react-aria${$b5e257d569688ac6$var$defaultContext.prefix}`;
|
|
121
|
+
return defaultId || `${prefix}-${id}`;
|
|
122
|
+
}
|
|
123
|
+
const $b5e257d569688ac6$export$619500959fc48b26 = typeof (0, $670gB$react)['useId'] === 'function' ? $b5e257d569688ac6$var$useModernSSRSafeId : $b5e257d569688ac6$var$useLegacySSRSafeId;
|
|
124
|
+
function $b5e257d569688ac6$var$getSnapshot() {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
function $b5e257d569688ac6$var$getServerSnapshot() {
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
131
|
+
function $b5e257d569688ac6$var$subscribe(onStoreChange) {
|
|
132
|
+
// noop
|
|
133
|
+
return ()=>{};
|
|
134
|
+
}
|
|
135
|
+
function $b5e257d569688ac6$export$535bd6ca7f90a273() {
|
|
136
|
+
// In React 18, we can use useSyncExternalStore to detect if we're server rendering or hydrating.
|
|
137
|
+
if (typeof (0, $670gB$react)['useSyncExternalStore'] === 'function') return (0, $670gB$react)['useSyncExternalStore']($b5e257d569688ac6$var$subscribe, $b5e257d569688ac6$var$getSnapshot, $b5e257d569688ac6$var$getServerSnapshot);
|
|
138
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
139
|
+
return (0, $670gB$useContext)($b5e257d569688ac6$var$IsSSRContext);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
export {$b5e257d569688ac6$export$9f8ac96af4b1b2ae as SSRProvider, $b5e257d569688ac6$export$535bd6ca7f90a273 as useIsSSR, $b5e257d569688ac6$export$619500959fc48b26 as useSSRSafeId};
|
|
144
|
+
//# sourceMappingURL=SSRProvider.module.js.map
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import $670gB$react, {useContext as $670gB$useContext, useState as $670gB$useState, useMemo as $670gB$useMemo, useLayoutEffect as $670gB$useLayoutEffect, useRef as $670gB$useRef} from "react";
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
+
* governing permissions and limitations under the License.
|
|
13
|
+
*/ // We must avoid a circular dependency with @react-aria/utils, and this useLayoutEffect is
|
|
14
|
+
// guarded by a check that it only runs on the client side.
|
|
15
|
+
// eslint-disable-next-line rulesdir/useLayoutEffectRule
|
|
16
|
+
|
|
17
|
+
// Default context value to use in case there is no SSRProvider. This is fine for
|
|
18
|
+
// client-only apps. In order to support multiple copies of React Aria potentially
|
|
19
|
+
// being on the page at once, the prefix is set to a random number. SSRProvider
|
|
20
|
+
// will reset this to zero for consistency between server and client, so in the
|
|
21
|
+
// SSR case multiple copies of React Aria is not supported.
|
|
22
|
+
const $b5e257d569688ac6$var$defaultContext = {
|
|
23
|
+
prefix: String(Math.round(Math.random() * 10000000000)),
|
|
24
|
+
current: 0
|
|
25
|
+
};
|
|
26
|
+
const $b5e257d569688ac6$var$SSRContext = /*#__PURE__*/ (0, $670gB$react).createContext($b5e257d569688ac6$var$defaultContext);
|
|
27
|
+
const $b5e257d569688ac6$var$IsSSRContext = /*#__PURE__*/ (0, $670gB$react).createContext(false);
|
|
28
|
+
// This is only used in React < 18.
|
|
29
|
+
function $b5e257d569688ac6$var$LegacySSRProvider(props) {
|
|
30
|
+
let cur = (0, $670gB$useContext)($b5e257d569688ac6$var$SSRContext);
|
|
31
|
+
let counter = $b5e257d569688ac6$var$useCounter(cur === $b5e257d569688ac6$var$defaultContext);
|
|
32
|
+
let [isSSR, setIsSSR] = (0, $670gB$useState)(true);
|
|
33
|
+
let value = (0, $670gB$useMemo)(()=>({
|
|
34
|
+
// If this is the first SSRProvider, start with an empty string prefix, otherwise
|
|
35
|
+
// append and increment the counter.
|
|
36
|
+
prefix: cur === $b5e257d569688ac6$var$defaultContext ? '' : `${cur.prefix}-${counter}`,
|
|
37
|
+
current: 0
|
|
38
|
+
}), [
|
|
39
|
+
cur,
|
|
40
|
+
counter
|
|
41
|
+
]);
|
|
42
|
+
// If on the client, and the component was initially server rendered,
|
|
43
|
+
// then schedule a layout effect to update the component after hydration.
|
|
44
|
+
if (typeof document !== 'undefined') // This if statement technically breaks the rules of hooks, but is safe
|
|
45
|
+
// because the condition never changes after mounting.
|
|
46
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
47
|
+
(0, $670gB$useLayoutEffect)(()=>{
|
|
48
|
+
setIsSSR(false);
|
|
49
|
+
}, []);
|
|
50
|
+
return /*#__PURE__*/ (0, $670gB$react).createElement($b5e257d569688ac6$var$SSRContext.Provider, {
|
|
51
|
+
value: value
|
|
52
|
+
}, /*#__PURE__*/ (0, $670gB$react).createElement($b5e257d569688ac6$var$IsSSRContext.Provider, {
|
|
53
|
+
value: isSSR
|
|
54
|
+
}, props.children));
|
|
55
|
+
}
|
|
56
|
+
let $b5e257d569688ac6$var$warnedAboutSSRProvider = false;
|
|
57
|
+
function $b5e257d569688ac6$export$9f8ac96af4b1b2ae(props) {
|
|
58
|
+
if (typeof (0, $670gB$react)['useId'] === 'function') {
|
|
59
|
+
if (process.env.NODE_ENV !== 'test' && !$b5e257d569688ac6$var$warnedAboutSSRProvider) {
|
|
60
|
+
console.warn('In React 18, SSRProvider is not necessary and is a noop. You can remove it from your app.');
|
|
61
|
+
$b5e257d569688ac6$var$warnedAboutSSRProvider = true;
|
|
62
|
+
}
|
|
63
|
+
return /*#__PURE__*/ (0, $670gB$react).createElement((0, $670gB$react).Fragment, null, props.children);
|
|
64
|
+
}
|
|
65
|
+
return /*#__PURE__*/ (0, $670gB$react).createElement($b5e257d569688ac6$var$LegacySSRProvider, props);
|
|
66
|
+
}
|
|
67
|
+
let $b5e257d569688ac6$var$canUseDOM = Boolean(typeof window !== 'undefined' && window.document && window.document.createElement);
|
|
68
|
+
let $b5e257d569688ac6$var$componentIds = new WeakMap();
|
|
69
|
+
function $b5e257d569688ac6$var$useCounter(isDisabled = false) {
|
|
70
|
+
let ctx = (0, $670gB$useContext)($b5e257d569688ac6$var$SSRContext);
|
|
71
|
+
let ref = (0, $670gB$useRef)(null);
|
|
72
|
+
// eslint-disable-next-line rulesdir/pure-render
|
|
73
|
+
if (ref.current === null && !isDisabled) {
|
|
74
|
+
var _React___SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED_ReactCurrentOwner, _React___SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
|
|
75
|
+
// In strict mode, React renders components twice, and the ref will be reset to null on the second render.
|
|
76
|
+
// This means our id counter will be incremented twice instead of once. This is a problem because on the
|
|
77
|
+
// server, components are only rendered once and so ids generated on the server won't match the client.
|
|
78
|
+
// In React 18, useId was introduced to solve this, but it is not available in older versions. So to solve this
|
|
79
|
+
// we need to use some React internals to access the underlying Fiber instance, which is stable between renders.
|
|
80
|
+
// This is exposed as ReactCurrentOwner in development, which is all we need since StrictMode only runs in development.
|
|
81
|
+
// To ensure that we only increment the global counter once, we store the starting id for this component in
|
|
82
|
+
// a weak map associated with the Fiber. On the second render, we reset the global counter to this value.
|
|
83
|
+
// Since React runs the second render immediately after the first, this is safe.
|
|
84
|
+
// @ts-ignore
|
|
85
|
+
let currentOwner = (_React___SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED = (0, $670gB$react).__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED) === null || _React___SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED === void 0 ? void 0 : (_React___SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED_ReactCurrentOwner = _React___SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner) === null || _React___SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED_ReactCurrentOwner === void 0 ? void 0 : _React___SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED_ReactCurrentOwner.current;
|
|
86
|
+
if (currentOwner) {
|
|
87
|
+
let prevComponentValue = $b5e257d569688ac6$var$componentIds.get(currentOwner);
|
|
88
|
+
if (prevComponentValue == null) // On the first render, and first call to useId, store the id and state in our weak map.
|
|
89
|
+
$b5e257d569688ac6$var$componentIds.set(currentOwner, {
|
|
90
|
+
id: ctx.current,
|
|
91
|
+
state: currentOwner.memoizedState
|
|
92
|
+
});
|
|
93
|
+
else if (currentOwner.memoizedState !== prevComponentValue.state) {
|
|
94
|
+
// On the second render, the memoizedState gets reset by React.
|
|
95
|
+
// Reset the counter, and remove from the weak map so we don't
|
|
96
|
+
// do this for subsequent useId calls.
|
|
97
|
+
ctx.current = prevComponentValue.id;
|
|
98
|
+
$b5e257d569688ac6$var$componentIds.delete(currentOwner);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
// eslint-disable-next-line rulesdir/pure-render
|
|
102
|
+
ref.current = ++ctx.current;
|
|
103
|
+
}
|
|
104
|
+
// eslint-disable-next-line rulesdir/pure-render
|
|
105
|
+
return ref.current;
|
|
106
|
+
}
|
|
107
|
+
function $b5e257d569688ac6$var$useLegacySSRSafeId(defaultId) {
|
|
108
|
+
let ctx = (0, $670gB$useContext)($b5e257d569688ac6$var$SSRContext);
|
|
109
|
+
// If we are rendering in a non-DOM environment, and there's no SSRProvider,
|
|
110
|
+
// provide a warning to hint to the developer to add one.
|
|
111
|
+
if (ctx === $b5e257d569688ac6$var$defaultContext && !$b5e257d569688ac6$var$canUseDOM) console.warn('When server rendering, you must wrap your application in an <SSRProvider> to ensure consistent ids are generated between the client and server.');
|
|
112
|
+
let counter = $b5e257d569688ac6$var$useCounter(!!defaultId);
|
|
113
|
+
let prefix = ctx === $b5e257d569688ac6$var$defaultContext && process.env.NODE_ENV === 'test' ? 'react-aria' : `react-aria${ctx.prefix}`;
|
|
114
|
+
return defaultId || `${prefix}-${counter}`;
|
|
115
|
+
}
|
|
116
|
+
function $b5e257d569688ac6$var$useModernSSRSafeId(defaultId) {
|
|
117
|
+
// @ts-ignore
|
|
118
|
+
let id = (0, $670gB$react).useId();
|
|
119
|
+
let [didSSR] = (0, $670gB$useState)($b5e257d569688ac6$export$535bd6ca7f90a273());
|
|
120
|
+
let prefix = didSSR || process.env.NODE_ENV === 'test' ? 'react-aria' : `react-aria${$b5e257d569688ac6$var$defaultContext.prefix}`;
|
|
121
|
+
return defaultId || `${prefix}-${id}`;
|
|
122
|
+
}
|
|
123
|
+
const $b5e257d569688ac6$export$619500959fc48b26 = typeof (0, $670gB$react)['useId'] === 'function' ? $b5e257d569688ac6$var$useModernSSRSafeId : $b5e257d569688ac6$var$useLegacySSRSafeId;
|
|
124
|
+
function $b5e257d569688ac6$var$getSnapshot() {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
function $b5e257d569688ac6$var$getServerSnapshot() {
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
131
|
+
function $b5e257d569688ac6$var$subscribe(onStoreChange) {
|
|
132
|
+
// noop
|
|
133
|
+
return ()=>{};
|
|
134
|
+
}
|
|
135
|
+
function $b5e257d569688ac6$export$535bd6ca7f90a273() {
|
|
136
|
+
// In React 18, we can use useSyncExternalStore to detect if we're server rendering or hydrating.
|
|
137
|
+
if (typeof (0, $670gB$react)['useSyncExternalStore'] === 'function') return (0, $670gB$react)['useSyncExternalStore']($b5e257d569688ac6$var$subscribe, $b5e257d569688ac6$var$getSnapshot, $b5e257d569688ac6$var$getServerSnapshot);
|
|
138
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
139
|
+
return (0, $670gB$useContext)($b5e257d569688ac6$var$IsSSRContext);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
export {$b5e257d569688ac6$export$9f8ac96af4b1b2ae as SSRProvider, $b5e257d569688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ssr/src/SSRProvider.tsx"],"sourcesContent":["/*\n * Copyright 2020 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\n// We must avoid a circular dependency with @react-aria/utils, and this useLayoutEffect is\n// guarded by a check that it only runs on the client side.\n// eslint-disable-next-line rulesdir/useLayoutEffectRule\nimport React, {JSX, ReactNode, useContext, useLayoutEffect, useMemo, useRef, useState} from 'react';\n\n// To support SSR, the auto incrementing id counter is stored in a context. This allows\n// it to be reset on every request to ensure the client and server are consistent.\n// There is also a prefix string that is used to support async loading components\n// Each async boundary must be wrapped in an SSR provider, which appends to the prefix\n// and resets the current id counter. This ensures that async loaded components have\n// consistent ids regardless of the loading order.\ninterface SSRContextValue {\n prefix: string,\n current: number\n}\n\n// Default context value to use in case there is no SSRProvider. This is fine for\n// client-only apps. In order to support multiple copies of React Aria potentially\n// being on the page at once, the prefix is set to a random number. SSRProvider\n// will reset this to zero for consistency between server and client, so in the\n// SSR case multiple copies of React Aria is not supported.\nconst defaultContext: SSRContextValue = {\n prefix: String(Math.round(Math.random() * 10000000000)),\n current: 0\n};\n\nconst SSRContext = React.createContext<SSRContextValue>(defaultContext);\nconst IsSSRContext = React.createContext(false);\n\nexport interface SSRProviderProps {\n /** Your application here. */\n children: ReactNode\n}\n\n// This is only used in React < 18.\nfunction LegacySSRProvider(props: SSRProviderProps): JSX.Element {\n let cur = useContext(SSRContext);\n let counter = useCounter(cur === defaultContext);\n let [isSSR, setIsSSR] = useState(true);\n let value: SSRContextValue = useMemo(() => ({\n // If this is the first SSRProvider, start with an empty string prefix, otherwise\n // append and increment the counter.\n prefix: cur === defaultContext ? '' : `${cur.prefix}-${counter}`,\n current: 0\n }), [cur, counter]);\n\n // If on the client, and the component was initially server rendered,\n // then schedule a layout effect to update the component after hydration.\n if (typeof document !== 'undefined') {\n // This if statement technically breaks the rules of hooks, but is safe\n // because the condition never changes after mounting.\n // eslint-disable-next-line react-hooks/rules-of-hooks\n useLayoutEffect(() => {\n setIsSSR(false);\n }, []);\n }\n\n return (\n <SSRContext.Provider value={value}>\n <IsSSRContext.Provider value={isSSR}>\n {props.children}\n </IsSSRContext.Provider>\n </SSRContext.Provider>\n );\n}\n\nlet warnedAboutSSRProvider = false;\n\n/**\n * When using SSR with React Aria in React 16 or 17, applications must be wrapped in an SSRProvider.\n * This ensures that auto generated ids are consistent between the client and server.\n */\nexport function SSRProvider(props: SSRProviderProps): JSX.Element {\n if (typeof React['useId'] === 'function') {\n if (process.env.NODE_ENV !== 'test' && !warnedAboutSSRProvider) {\n console.warn('In React 18, SSRProvider is not necessary and is a noop. You can remove it from your app.');\n warnedAboutSSRProvider = true;\n }\n return <>{props.children}</>;\n }\n return <LegacySSRProvider {...props} />;\n}\n\nlet canUseDOM = Boolean(\n typeof window !== 'undefined' &&\n window.document &&\n window.document.createElement\n);\n\nlet componentIds = new WeakMap();\n\nfunction useCounter(isDisabled = false) {\n let ctx = useContext(SSRContext);\n let ref = useRef<number | null>(null);\n // eslint-disable-next-line rulesdir/pure-render\n if (ref.current === null && !isDisabled) {\n // In strict mode, React renders components twice, and the ref will be reset to null on the second render.\n // This means our id counter will be incremented twice instead of once. This is a problem because on the\n // server, components are only rendered once and so ids generated on the server won't match the client.\n // In React 18, useId was introduced to solve this, but it is not available in older versions. So to solve this\n // we need to use some React internals to access the underlying Fiber instance, which is stable between renders.\n // This is exposed as ReactCurrentOwner in development, which is all we need since StrictMode only runs in development.\n // To ensure that we only increment the global counter once, we store the starting id for this component in\n // a weak map associated with the Fiber. On the second render, we reset the global counter to this value.\n // Since React runs the second render immediately after the first, this is safe.\n // @ts-ignore\n let currentOwner = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED?.ReactCurrentOwner?.current;\n if (currentOwner) {\n let prevComponentValue = componentIds.get(currentOwner);\n if (prevComponentValue == null) {\n // On the first render, and first call to useId, store the id and state in our weak map.\n componentIds.set(currentOwner, {\n id: ctx.current,\n state: currentOwner.memoizedState\n });\n } else if (currentOwner.memoizedState !== prevComponentValue.state) {\n // On the second render, the memoizedState gets reset by React.\n // Reset the counter, and remove from the weak map so we don't\n // do this for subsequent useId calls.\n ctx.current = prevComponentValue.id;\n componentIds.delete(currentOwner);\n }\n }\n\n // eslint-disable-next-line rulesdir/pure-render\n ref.current = ++ctx.current;\n }\n\n // eslint-disable-next-line rulesdir/pure-render\n return ref.current;\n}\n\nfunction useLegacySSRSafeId(defaultId?: string): string {\n let ctx = useContext(SSRContext);\n\n // If we are rendering in a non-DOM environment, and there's no SSRProvider,\n // provide a warning to hint to the developer to add one.\n if (ctx === defaultContext && !canUseDOM) {\n console.warn('When server rendering, you must wrap your application in an <SSRProvider> to ensure consistent ids are generated between the client and server.');\n }\n\n let counter = useCounter(!!defaultId);\n let prefix = ctx === defaultContext && process.env.NODE_ENV === 'test' ? 'react-aria' : `react-aria${ctx.prefix}`;\n return defaultId || `${prefix}-${counter}`;\n}\n\nfunction useModernSSRSafeId(defaultId?: string): string {\n // @ts-ignore\n let id = React.useId();\n let [didSSR] = useState(useIsSSR());\n let prefix = didSSR || process.env.NODE_ENV === 'test' ? 'react-aria' : `react-aria${defaultContext.prefix}`;\n return defaultId || `${prefix}-${id}`;\n}\n\n// Use React.useId in React 18 if available, otherwise fall back to our old implementation.\n/** @private */\nexport const useSSRSafeId = typeof React['useId'] === 'function' ? useModernSSRSafeId : useLegacySSRSafeId;\n\nfunction getSnapshot() {\n return false;\n}\n\nfunction getServerSnapshot() {\n return true;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nfunction subscribe(onStoreChange: () => void): () => void {\n // noop\n return () => {};\n}\n\n/**\n * Returns whether the component is currently being server side rendered or\n * hydrated on the client. Can be used to delay browser-specific rendering\n * until after hydration.\n */\nexport function useIsSSR(): boolean {\n // In React 18, we can use useSyncExternalStore to detect if we're server rendering or hydrating.\n if (typeof React['useSyncExternalStore'] === 'function') {\n return React['useSyncExternalStore'](subscribe, getSnapshot, getServerSnapshot);\n }\n\n // eslint-disable-next-line react-hooks/rules-of-hooks\n return useContext(IsSSRContext);\n}\n"],"names":[],"version":3,"file":"SSRProvider.module.js.map","sourceRoot":"../../../../"}
|
package/dist/import.mjs
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {SSRProvider as $b5e257d569688ac6$export$9f8ac96af4b1b2ae, useIsSSR as $b5e257d569688ac6$export$535bd6ca7f90a273, useSSRSafeId as $b5e257d569688ac6$export$619500959fc48b26} from "./SSRProvider.mjs";
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
+
* governing permissions and limitations under the License.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
export {$b5e257d569688ac6$export$9f8ac96af4b1b2ae as SSRProvider, $b5e257d569688ac6$export$619500959fc48b26 as useSSRSafeId, $b5e257d569688ac6$export$535bd6ca7f90a273 as useIsSSR};
|
|
17
|
+
//# sourceMappingURL=module.js.map
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
var $97d95f6660b1bb14$exports = require("./SSRProvider.main.js");
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
function $parcel$export(e, n, v, s) {
|
|
5
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
$parcel$export(module.exports, "SSRProvider", () => $97d95f6660b1bb14$exports.SSRProvider);
|
|
9
|
+
$parcel$export(module.exports, "useSSRSafeId", () => $97d95f6660b1bb14$exports.useSSRSafeId);
|
|
10
|
+
$parcel$export(module.exports, "useIsSSR", () => $97d95f6660b1bb14$exports.useIsSSR);
|
|
11
|
+
/*
|
|
12
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
13
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
14
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
15
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
16
|
+
*
|
|
17
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
18
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
19
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
20
|
+
* governing permissions and limitations under the License.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;;;;;;;;;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-aria/ssr/src/index.ts"],"sourcesContent":["/*\n * Copyright 2020 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 */\nexport {SSRProvider, useSSRSafeId, useIsSSR} from './SSRProvider';\nexport type {SSRProviderProps} from './SSRProvider';\n"],"names":[],"version":3,"file":"main.js.map","sourceRoot":"../../../../"}
|
package/dist/module.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {SSRProvider as $b5e257d569688ac6$export$9f8ac96af4b1b2ae, useIsSSR as $b5e257d569688ac6$export$535bd6ca7f90a273, useSSRSafeId as $b5e257d569688ac6$export$619500959fc48b26} from "./SSRProvider.module.js";
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
+
* governing permissions and limitations under the License.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
export {$b5e257d569688ac6$export$9f8ac96af4b1b2ae as SSRProvider, $b5e257d569688ac6$export$619500959fc48b26 as useSSRSafeId, $b5e257d569688ac6$export$535bd6ca7f90a273 as useIsSSR};
|
|
17
|
+
//# sourceMappingURL=module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-aria/ssr/src/index.ts"],"sourcesContent":["/*\n * Copyright 2020 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 */\nexport {SSRProvider, useSSRSafeId, useIsSSR} from './SSRProvider';\nexport type {SSRProviderProps} from './SSRProvider';\n"],"names":[],"version":3,"file":"module.js.map","sourceRoot":"../../../../"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { JSX, ReactNode } from "react";
|
|
2
|
+
export interface SSRProviderProps {
|
|
3
|
+
/** Your application here. */
|
|
4
|
+
children: ReactNode;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* When using SSR with React Aria in React 16 or 17, applications must be wrapped in an SSRProvider.
|
|
8
|
+
* This ensures that auto generated ids are consistent between the client and server.
|
|
9
|
+
*/
|
|
10
|
+
export function SSRProvider(props: SSRProviderProps): JSX.Element;
|
|
11
|
+
declare function useModernSSRSafeId(defaultId?: string): string;
|
|
12
|
+
/** @private */
|
|
13
|
+
export const useSSRSafeId: typeof useModernSSRSafeId;
|
|
14
|
+
/**
|
|
15
|
+
* Returns whether the component is currently being server side rendered or
|
|
16
|
+
* hydrated on the client. Can be used to delay browser-specific rendering
|
|
17
|
+
* until after hydration.
|
|
18
|
+
*/
|
|
19
|
+
export function useIsSSR(): boolean;
|
|
20
|
+
|
|
21
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";AAyCA;IACE,6BAA6B;IAC7B,QAAQ,EAAE,SAAS,CAAA;CACpB;AAoCD;;;GAGG;AACH,4BAA4B,KAAK,EAAE,gBAAgB,GAAG,IAAI,OAAO,CAShE;AAiED,oCAA4B,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,CAMtD;AAGD,eAAe;AACf,OAAO,MAAM,uCAA6F,CAAC;AAgB3G;;;;GAIG;AACH,4BAA4B,OAAO,CAQlC","sources":["packages/@react-aria/ssr/src/packages/@react-aria/ssr/src/SSRProvider.tsx","packages/@react-aria/ssr/src/packages/@react-aria/ssr/src/index.ts","packages/@react-aria/ssr/src/index.ts"],"sourcesContent":[null,null,"/*\n * Copyright 2020 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 */\nexport {SSRProvider, useSSRSafeId, useIsSSR} from './SSRProvider';\nexport type {SSRProviderProps} from './SSRProvider';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@react-aria/ssr",
|
|
3
|
+
"version": "3.0.0-nightly-641446f65-240905",
|
|
4
|
+
"description": "Spectrum UI components in React",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"main": "dist/main.js",
|
|
7
|
+
"module": "dist/module.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
"types": "./dist/types.d.ts",
|
|
10
|
+
"import": "./dist/import.mjs",
|
|
11
|
+
"require": "./dist/main.js"
|
|
12
|
+
},
|
|
13
|
+
"types": "dist/types.d.ts",
|
|
14
|
+
"source": "src/index.ts",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src"
|
|
18
|
+
],
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/adobe/react-spectrum"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@swc/helpers": "^0.5.0"
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">= 12"
|
|
35
|
+
},
|
|
36
|
+
"targets": {
|
|
37
|
+
"main": {
|
|
38
|
+
"context": "node"
|
|
39
|
+
},
|
|
40
|
+
"module": {
|
|
41
|
+
"context": "node"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"stableVersion": "3.9.5"
|
|
45
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
// We must avoid a circular dependency with @react-aria/utils, and this useLayoutEffect is
|
|
14
|
+
// guarded by a check that it only runs on the client side.
|
|
15
|
+
// eslint-disable-next-line rulesdir/useLayoutEffectRule
|
|
16
|
+
import React, {JSX, ReactNode, useContext, useLayoutEffect, useMemo, useRef, useState} from 'react';
|
|
17
|
+
|
|
18
|
+
// To support SSR, the auto incrementing id counter is stored in a context. This allows
|
|
19
|
+
// it to be reset on every request to ensure the client and server are consistent.
|
|
20
|
+
// There is also a prefix string that is used to support async loading components
|
|
21
|
+
// Each async boundary must be wrapped in an SSR provider, which appends to the prefix
|
|
22
|
+
// and resets the current id counter. This ensures that async loaded components have
|
|
23
|
+
// consistent ids regardless of the loading order.
|
|
24
|
+
interface SSRContextValue {
|
|
25
|
+
prefix: string,
|
|
26
|
+
current: number
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Default context value to use in case there is no SSRProvider. This is fine for
|
|
30
|
+
// client-only apps. In order to support multiple copies of React Aria potentially
|
|
31
|
+
// being on the page at once, the prefix is set to a random number. SSRProvider
|
|
32
|
+
// will reset this to zero for consistency between server and client, so in the
|
|
33
|
+
// SSR case multiple copies of React Aria is not supported.
|
|
34
|
+
const defaultContext: SSRContextValue = {
|
|
35
|
+
prefix: String(Math.round(Math.random() * 10000000000)),
|
|
36
|
+
current: 0
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const SSRContext = React.createContext<SSRContextValue>(defaultContext);
|
|
40
|
+
const IsSSRContext = React.createContext(false);
|
|
41
|
+
|
|
42
|
+
export interface SSRProviderProps {
|
|
43
|
+
/** Your application here. */
|
|
44
|
+
children: ReactNode
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// This is only used in React < 18.
|
|
48
|
+
function LegacySSRProvider(props: SSRProviderProps): JSX.Element {
|
|
49
|
+
let cur = useContext(SSRContext);
|
|
50
|
+
let counter = useCounter(cur === defaultContext);
|
|
51
|
+
let [isSSR, setIsSSR] = useState(true);
|
|
52
|
+
let value: SSRContextValue = useMemo(() => ({
|
|
53
|
+
// If this is the first SSRProvider, start with an empty string prefix, otherwise
|
|
54
|
+
// append and increment the counter.
|
|
55
|
+
prefix: cur === defaultContext ? '' : `${cur.prefix}-${counter}`,
|
|
56
|
+
current: 0
|
|
57
|
+
}), [cur, counter]);
|
|
58
|
+
|
|
59
|
+
// If on the client, and the component was initially server rendered,
|
|
60
|
+
// then schedule a layout effect to update the component after hydration.
|
|
61
|
+
if (typeof document !== 'undefined') {
|
|
62
|
+
// This if statement technically breaks the rules of hooks, but is safe
|
|
63
|
+
// because the condition never changes after mounting.
|
|
64
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
65
|
+
useLayoutEffect(() => {
|
|
66
|
+
setIsSSR(false);
|
|
67
|
+
}, []);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return (
|
|
71
|
+
<SSRContext.Provider value={value}>
|
|
72
|
+
<IsSSRContext.Provider value={isSSR}>
|
|
73
|
+
{props.children}
|
|
74
|
+
</IsSSRContext.Provider>
|
|
75
|
+
</SSRContext.Provider>
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
let warnedAboutSSRProvider = false;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* When using SSR with React Aria in React 16 or 17, applications must be wrapped in an SSRProvider.
|
|
83
|
+
* This ensures that auto generated ids are consistent between the client and server.
|
|
84
|
+
*/
|
|
85
|
+
export function SSRProvider(props: SSRProviderProps): JSX.Element {
|
|
86
|
+
if (typeof React['useId'] === 'function') {
|
|
87
|
+
if (process.env.NODE_ENV !== 'test' && !warnedAboutSSRProvider) {
|
|
88
|
+
console.warn('In React 18, SSRProvider is not necessary and is a noop. You can remove it from your app.');
|
|
89
|
+
warnedAboutSSRProvider = true;
|
|
90
|
+
}
|
|
91
|
+
return <>{props.children}</>;
|
|
92
|
+
}
|
|
93
|
+
return <LegacySSRProvider {...props} />;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
let canUseDOM = Boolean(
|
|
97
|
+
typeof window !== 'undefined' &&
|
|
98
|
+
window.document &&
|
|
99
|
+
window.document.createElement
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
let componentIds = new WeakMap();
|
|
103
|
+
|
|
104
|
+
function useCounter(isDisabled = false) {
|
|
105
|
+
let ctx = useContext(SSRContext);
|
|
106
|
+
let ref = useRef<number | null>(null);
|
|
107
|
+
// eslint-disable-next-line rulesdir/pure-render
|
|
108
|
+
if (ref.current === null && !isDisabled) {
|
|
109
|
+
// In strict mode, React renders components twice, and the ref will be reset to null on the second render.
|
|
110
|
+
// This means our id counter will be incremented twice instead of once. This is a problem because on the
|
|
111
|
+
// server, components are only rendered once and so ids generated on the server won't match the client.
|
|
112
|
+
// In React 18, useId was introduced to solve this, but it is not available in older versions. So to solve this
|
|
113
|
+
// we need to use some React internals to access the underlying Fiber instance, which is stable between renders.
|
|
114
|
+
// This is exposed as ReactCurrentOwner in development, which is all we need since StrictMode only runs in development.
|
|
115
|
+
// To ensure that we only increment the global counter once, we store the starting id for this component in
|
|
116
|
+
// a weak map associated with the Fiber. On the second render, we reset the global counter to this value.
|
|
117
|
+
// Since React runs the second render immediately after the first, this is safe.
|
|
118
|
+
// @ts-ignore
|
|
119
|
+
let currentOwner = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED?.ReactCurrentOwner?.current;
|
|
120
|
+
if (currentOwner) {
|
|
121
|
+
let prevComponentValue = componentIds.get(currentOwner);
|
|
122
|
+
if (prevComponentValue == null) {
|
|
123
|
+
// On the first render, and first call to useId, store the id and state in our weak map.
|
|
124
|
+
componentIds.set(currentOwner, {
|
|
125
|
+
id: ctx.current,
|
|
126
|
+
state: currentOwner.memoizedState
|
|
127
|
+
});
|
|
128
|
+
} else if (currentOwner.memoizedState !== prevComponentValue.state) {
|
|
129
|
+
// On the second render, the memoizedState gets reset by React.
|
|
130
|
+
// Reset the counter, and remove from the weak map so we don't
|
|
131
|
+
// do this for subsequent useId calls.
|
|
132
|
+
ctx.current = prevComponentValue.id;
|
|
133
|
+
componentIds.delete(currentOwner);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// eslint-disable-next-line rulesdir/pure-render
|
|
138
|
+
ref.current = ++ctx.current;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// eslint-disable-next-line rulesdir/pure-render
|
|
142
|
+
return ref.current;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function useLegacySSRSafeId(defaultId?: string): string {
|
|
146
|
+
let ctx = useContext(SSRContext);
|
|
147
|
+
|
|
148
|
+
// If we are rendering in a non-DOM environment, and there's no SSRProvider,
|
|
149
|
+
// provide a warning to hint to the developer to add one.
|
|
150
|
+
if (ctx === defaultContext && !canUseDOM) {
|
|
151
|
+
console.warn('When server rendering, you must wrap your application in an <SSRProvider> to ensure consistent ids are generated between the client and server.');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
let counter = useCounter(!!defaultId);
|
|
155
|
+
let prefix = ctx === defaultContext && process.env.NODE_ENV === 'test' ? 'react-aria' : `react-aria${ctx.prefix}`;
|
|
156
|
+
return defaultId || `${prefix}-${counter}`;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function useModernSSRSafeId(defaultId?: string): string {
|
|
160
|
+
// @ts-ignore
|
|
161
|
+
let id = React.useId();
|
|
162
|
+
let [didSSR] = useState(useIsSSR());
|
|
163
|
+
let prefix = didSSR || process.env.NODE_ENV === 'test' ? 'react-aria' : `react-aria${defaultContext.prefix}`;
|
|
164
|
+
return defaultId || `${prefix}-${id}`;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Use React.useId in React 18 if available, otherwise fall back to our old implementation.
|
|
168
|
+
/** @private */
|
|
169
|
+
export const useSSRSafeId = typeof React['useId'] === 'function' ? useModernSSRSafeId : useLegacySSRSafeId;
|
|
170
|
+
|
|
171
|
+
function getSnapshot() {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function getServerSnapshot() {
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
180
|
+
function subscribe(onStoreChange: () => void): () => void {
|
|
181
|
+
// noop
|
|
182
|
+
return () => {};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Returns whether the component is currently being server side rendered or
|
|
187
|
+
* hydrated on the client. Can be used to delay browser-specific rendering
|
|
188
|
+
* until after hydration.
|
|
189
|
+
*/
|
|
190
|
+
export function useIsSSR(): boolean {
|
|
191
|
+
// In React 18, we can use useSyncExternalStore to detect if we're server rendering or hydrating.
|
|
192
|
+
if (typeof React['useSyncExternalStore'] === 'function') {
|
|
193
|
+
return React['useSyncExternalStore'](subscribe, getSnapshot, getServerSnapshot);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
197
|
+
return useContext(IsSSRContext);
|
|
198
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
export {SSRProvider, useSSRSafeId, useIsSSR} from './SSRProvider';
|
|
13
|
+
export type {SSRProviderProps} from './SSRProvider';
|