Skip to content

Commit ab36c74

Browse files
committed
Another attempt
1 parent d147e7b commit ab36c74

File tree

4 files changed

+26
-27
lines changed

4 files changed

+26
-27
lines changed

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
**/components/stories
2+
**/components/tests

src/components/Space.tsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
import { CenterType, ResizeHandlePlacement, AnchorType, Type } from "../core-types";
2-
import {
3-
useSpace,
4-
ParentContext,
5-
LayerContext,
6-
DOMRectContext,
7-
IReactSpaceInnerProps,
8-
useEffectOnce,
9-
SSR_SUPPORT_ENABLED,
10-
useUniqueId,
11-
} from "../core-react";
2+
import { useSpace, ParentContext, LayerContext, DOMRectContext, IReactSpaceInnerProps, useEffectOnce, SSR_SUPPORT_ENABLED } from "../core-react";
123
import * as React from "react";
134
import { Centered } from "./Centered";
145
import { CenteredVertically } from "./CenteredVertically";
156
import { isServer, updateStyleDefinition } from "../core-utils";
7+
import { useUniqueId } from "src/core-react-interop";
168

179
function applyCentering(children: React.ReactNode, centerType: CenterType | undefined) {
1810
switch (centerType) {
@@ -41,7 +33,7 @@ const SpaceInner: React.FC<IReactSpaceInnerProps & { wrapperInstance: Space }> =
4133
let idToUse = props.id ?? getSpaceUniqueId(props.wrapperInstance);
4234
const [initialRender, setInitialRender] = React.useState(SSR_SUPPORT_ENABLED ? true : false);
4335

44-
const uniqueId = useUniqueId();
36+
const uniqueId = useUniqueId(SSR_SUPPORT_ENABLED);
4537

4638
if (!idToUse) {
4739
setSpaceUniqueId(props.wrapperInstance, uniqueId);

src/core-react-interop.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { shortuuid } from "./core-utils";
2+
const React = require("react");
3+
4+
export function useUniqueId(ssrEnabled: boolean) {
5+
if (ssrEnabled) {
6+
if (React.version.startsWith("18") && typeof React.useId !== "undefined") {
7+
return `s${React.useId().replace(/\:/g, "")}`;
8+
}
9+
10+
// @ts-ignore
11+
if (typeof React.unstable_useOpaqueIdentifier !== "undefined") {
12+
// @ts-ignore
13+
return `s${React.unstable_useOpaqueIdentifier().replace(/\:/g, "")}`;
14+
}
15+
}
16+
17+
return `s${shortuuid()}`;
18+
}

src/core-react.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import {
1212
OnDragEnd,
1313
ResizeTouchEvent,
1414
} from "./core-types";
15-
import { coalesce, shortuuid } from "./core-utils";
15+
import { coalesce } from "./core-utils";
1616
import { ResizeSensor } from "css-element-queries";
1717
import * as PropTypes from "prop-types";
1818
import { useEffect, useRef, useState } from "react";
19+
import { useUniqueId } from "./core-react-interop";
1920

2021
// WORKAROUND for React18 strict mode
2122
// https://blog.ag-grid.com/avoiding-react-18-double-mount/
@@ -118,27 +119,13 @@ export function useForceUpdate() {
118119
}, []);
119120
}
120121

121-
export function useUniqueId() {
122-
if (SSR_SUPPORT_ENABLED) {
123-
if (React.version.startsWith("18") && typeof (React as any)["useId"] !== "undefined") {
124-
return `s${(React as any)["useId"]().replace(/\:/g, "")}`;
125-
}
126-
127-
if (typeof (React as any)["unstable_useOpaqueIdentifier"] !== "undefined") {
128-
return `s${(React as any)["unstable_useOpaqueIdentifier"]().replace(/\:/g, "")}`;
129-
}
130-
}
131-
132-
return `s${shortuuid()}`;
133-
}
134-
135122
export function useSpace(props: IReactSpaceInnerProps) {
136123
const store = currentStore;
137124
const update = useForceUpdate();
138125
const parent = React.useContext(ParentContext);
139126
const layer = React.useContext(LayerContext);
140127
const { debug } = React.useContext(OptionsContext);
141-
const uniqueId = useUniqueId();
128+
const uniqueId = useUniqueId(SSR_SUPPORT_ENABLED);
142129
const [spaceId] = React.useState(props.id || uniqueId);
143130
const elementRef = React.useRef<HTMLElement>();
144131
const resizeSensor = React.useRef<ResizeSensor>();

0 commit comments

Comments
 (0)