Skip to content

Commit 23020a0

Browse files
committed
fix: extract document fetching prop into util (#259)
1 parent 8ec1119 commit 23020a0

File tree

8 files changed

+31
-31
lines changed

8 files changed

+31
-31
lines changed

next-release-notes.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,2 @@
1-
<!--
2-
### Breaking Changes
3-
4-
### Features
5-
61
### Bug Fixes and Improvements
7-
8-
### Other Changes
9-
-->
2+
- Fixed globally-scoped references to the document prop into a getter that doesn't fail in ssr environments (#259)

packages/core/src/controlledEnvironment/layoutUtils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { getDocument } from '../utils';
2+
13
export const computeItemHeight = (treeId: string) => {
2-
const firstItem = document.querySelector<HTMLElement>(
4+
const firstItem = getDocument()?.querySelector<HTMLElement>(
35
`[data-rct-tree="${treeId}"] [data-rct-item-container="true"]`
46
);
57
return firstItem?.offsetHeight ?? 5;

packages/core/src/controlledEnvironment/useControlledTreeEnvironmentProps.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from '../types';
99
import { scrollIntoView } from '../tree/scrollIntoView';
1010
import { useRenderers } from '../renderers/useRenderers';
11-
import { buildMapForTrees } from '../utils';
11+
import { buildMapForTrees, getDocument } from '../utils';
1212
import { getItemsLinearly } from '../tree/getItemsLinearly';
1313
import { useRefCopy } from '../useRefCopy';
1414
import { useStableHandler } from '../use-stable-handler';
@@ -53,13 +53,13 @@ export const useControlledTreeEnvironmentProps = ({
5353
Required<TreeChangeHandlers>['onFocusItem']
5454
>(
5555
(item, treeId) => {
56-
const newItem = document.querySelector(
56+
const newItem = getDocument()?.querySelector(
5757
`[data-rct-tree="${treeId}"] [data-rct-item-id="${item.index}"]`
5858
);
5959

6060
if (autoFocus ?? true) {
6161
if (
62-
document.activeElement?.attributes.getNamedItem(
62+
getDocument()?.activeElement?.attributes.getNamedItem(
6363
'data-rct-search-input'
6464
)?.value !== 'true'
6565
) {
@@ -123,7 +123,7 @@ export const useControlledTreeEnvironmentProps = ({
123123
);
124124

125125
const focusTree = useCallback((treeId: string) => {
126-
const focusItem = document.querySelector(
126+
const focusItem = getDocument()?.querySelector(
127127
`[data-rct-tree="${treeId}"] [data-rct-item-focus="true"]`
128128
);
129129
(focusItem as HTMLElement)?.focus?.();
@@ -136,8 +136,8 @@ export const useControlledTreeEnvironmentProps = ({
136136
autoFocusTree &&
137137
(autoFocus ?? true) &&
138138
treeId &&
139-
!document
140-
.querySelector(`[data-rct-tree="${treeId}"]`)
139+
!getDocument()
140+
?.querySelector(`[data-rct-tree="${treeId}"]`)
141141
?.contains(document.activeElement)
142142
) {
143143
focusTree(treeId);

packages/core/src/hotkeys/useHotkey.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useHtmlElementEventListener } from '../useHtmlElementEventListener';
33
import { KeyboardBindings } from '../types';
44
import { useKeyboardBindings } from './useKeyboardBindings';
55
import { useCallSoon } from '../useCallSoon';
6+
import { getDocument } from '../utils';
67

78
const elementsThatCanTakeText = ['input', 'textarea'];
89

@@ -24,7 +25,7 @@ export const useHotkey = (
2425
[combinationName, keyboardBindings]
2526
);
2627

27-
useHtmlElementEventListener(document, 'keydown', e => {
28+
useHtmlElementEventListener(getDocument(), 'keydown', e => {
2829
if (active === false) {
2930
return;
3031
}
@@ -63,7 +64,7 @@ export const useHotkey = (
6364
}
6465
});
6566

66-
useHtmlElementEventListener(document, 'keyup', e => {
67+
useHtmlElementEventListener(getDocument(), 'keyup', e => {
6768
if (active === false) {
6869
return;
6970
}

packages/core/src/hotkeys/useKey.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
import { useHtmlElementEventListener } from '../useHtmlElementEventListener';
2+
import { getDocument } from '../utils';
23

34
export const useKey = (
45
key: string,
56
onHit: (e: KeyboardEvent) => void,
67
active?: boolean
78
) => {
8-
useHtmlElementEventListener(
9-
typeof document !== 'undefined' ? document : undefined,
10-
'keydown',
11-
e => {
12-
if (!active) {
13-
return;
14-
}
9+
useHtmlElementEventListener(getDocument(), 'keydown', e => {
10+
if (!active) {
11+
return;
12+
}
1513

16-
if (active && key.toLowerCase() === e.key.toLowerCase()) {
17-
onHit(e);
18-
}
14+
if (active && key.toLowerCase() === e.key.toLowerCase()) {
15+
onHit(e);
1916
}
20-
);
17+
});
2118
};

packages/core/src/search/SearchInput.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useTreeEnvironment } from '../controlledEnvironment/ControlledTreeEnvir
66
import { useSearchMatchFocus } from './useSearchMatchFocus';
77
import { useViewState } from '../tree/useViewState';
88
import { useCallSoon } from '../useCallSoon';
9+
import { getDocument } from '../utils';
910

1011
export const SearchInput: React.FC<{
1112
containerRef?: HTMLElement;
@@ -24,7 +25,7 @@ export const SearchInput: React.FC<{
2425
if (environment.autoFocus ?? true) {
2526
// Refocus item in tree
2627
// TODO move logic as reusable method into tree or tree environment
27-
const focusItem = document.querySelector(
28+
const focusItem = getDocument()?.querySelector(
2829
`[data-rct-tree="${treeId}"] [data-rct-item-focus="true"]`
2930
);
3031
(focusItem as HTMLElement)?.focus?.();

packages/core/src/tree/scrollIntoView.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { getDocument } from '../utils';
2+
13
export const scrollIntoView = (element: Element | undefined | null) => {
24
if (!element) {
35
return;
@@ -11,9 +13,10 @@ export const scrollIntoView = (element: Element | undefined | null) => {
1113
boundingBox.top >= 0 &&
1214
boundingBox.left >= 0 &&
1315
boundingBox.bottom <=
14-
(window.innerHeight || document.documentElement.clientHeight) &&
16+
(window.innerHeight ||
17+
!!getDocument()?.documentElement?.clientHeight) &&
1518
boundingBox.right <=
16-
(window.innerWidth || document.documentElement.clientWidth);
19+
(window.innerWidth || !!getDocument()?.documentElement?.clientWidth);
1720
if (!isElementInViewport) {
1821
element.scrollIntoView();
1922
}

packages/core/src/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ export const buildMapForTrees = <T>(
55
treeIds
66
.map(id => [id, build(id)] as const)
77
.reduce((a, [id, obj]) => ({ ...a, [id]: obj }), {});
8+
9+
export const getDocument = () =>
10+
typeof document !== 'undefined' ? document : undefined;

0 commit comments

Comments
 (0)