Skip to content

Commit 29d251a

Browse files
committed
feat: better useAutoheight debugging experience regarding incompatible WebView props
1 parent 998f256 commit 29d251a

File tree

1 file changed

+68
-33
lines changed

1 file changed

+68
-33
lines changed

packages/webshell/src/hooks/useAutoheight.ts

+68-33
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ import Feature from '../Feature';
1515

1616
const initialDimensions = { width: undefined, height: undefined };
1717

18-
let numberOfEvents = 0;
18+
const overridenWebViewProps = {
19+
scalesPageToFit: false,
20+
showsVerticalScrollIndicator: false,
21+
disableScrollViewPanResponder: true,
22+
contentMode: 'mobile'
23+
} as const;
24+
25+
const overridenWebViewKeys = Object.keys(overridenWebViewProps);
1926

2027
/**
2128
* The state of synchronization between viewport and content size:
@@ -131,20 +138,69 @@ const initialState: AutoheightInternalState = {
131138
viewportWidth: 0
132139
};
133140

141+
function useDevFeedbackEffect({
142+
autoHeightParams: { webshellProps },
143+
state: {
144+
implementation,
145+
contentSize: { width, height }
146+
}
147+
}: {
148+
autoHeightParams: AutoheightParams<WebshellProps<MinimalWebViewProps, []>>;
149+
state: AutoheightInternalState;
150+
}) {
151+
const numberOfEventsRef = React.useRef(0);
152+
const { webshellDebug } = webshellProps;
153+
const forbiddenWebViewProps = overridenWebViewKeys
154+
.map((key) => [key, webshellProps[key]])
155+
.reduce((prev, [key, value]) => {
156+
prev[key] = value;
157+
return prev;
158+
}, {} as any);
159+
React.useEffect(
160+
function warnOverridenProps() {
161+
for (const forbiddenKey of overridenWebViewKeys) {
162+
if (
163+
forbiddenWebViewProps[forbiddenKey] !== undefined &&
164+
forbiddenWebViewProps[forbiddenKey] !==
165+
overridenWebViewProps[forbiddenKey]
166+
) {
167+
console.warn(
168+
`${useAutoheight.name}: You cannot set "${forbiddenKey}" prop to "${webshellProps[forbiddenKey]}" with autoheight hook. The value will be overriden to "${overridenWebViewProps[forbiddenKey]}".`
169+
);
170+
}
171+
}
172+
},
173+
// eslint-disable-next-line react-hooks/exhaustive-deps
174+
[webshellDebug, ...Object.values(forbiddenWebViewProps)]
175+
);
176+
React.useEffect(
177+
function debugDOMHTMLDimensions() {
178+
webshellDebug &&
179+
console.info(
180+
`${
181+
useAutoheight.name
182+
}: DOMHTMLDimensions event #${++numberOfEventsRef.current} (implementation: ${implementation}, content width: ${width}, content height: ${height})`
183+
);
184+
},
185+
[webshellDebug, implementation, height, width]
186+
);
187+
}
188+
134189
function useAutoheightState<
135190
S extends WebshellProps<
136191
MinimalWebViewProps,
137192
[ExtractFeatureFromClass<typeof HandleHTMLDimensionsFeature>]
138193
>
139-
>({ webshellProps, initialHeight }: AutoheightParams<S>) {
140-
const { scalesPageToFit, source = {}, webshellDebug } = webshellProps;
194+
>(autoHeightParams: AutoheightParams<S>) {
195+
const { webshellProps, initialHeight } = autoHeightParams;
196+
const { source = {}, webshellDebug } = webshellProps;
141197
const [state, setState] = React.useState<AutoheightInternalState>(
142198
initialState
143199
);
144-
const {
145-
implementation,
146-
contentSize: { width, height }
147-
} = state;
200+
if (__DEV__) {
201+
// eslint-disable-next-line react-hooks/rules-of-hooks
202+
useDevFeedbackEffect({ autoHeightParams, state });
203+
}
148204
React.useEffect(
149205
function resetHeightOnSourceChanges() {
150206
setState(({ contentSize, viewportWidth }) => ({
@@ -157,34 +213,16 @@ function useAutoheightState<
157213
syncState: 'syncing',
158214
lastFrameChangedWidth: false
159215
}));
160-
webshellDebug &&
216+
__DEV__ &&
217+
webshellDebug &&
161218
console.info(
162219
`${useAutoheight.name}: source change detected, resetting height to ${initialHeight}dp.`
163220
);
164221
},
165222
[source.uri, source.html, webshellDebug, initialHeight]
166223
);
167-
React.useEffect(
168-
function debugScalesPageToFit() {
169-
webshellDebug &&
170-
scalesPageToFit === true &&
171-
console.warn(
172-
`${useAutoheight.name}: You cannot use scalesPageToFit with autoheight hook. The value will be overriden to false`
173-
);
174-
},
175-
[scalesPageToFit, webshellDebug]
176-
);
177-
React.useEffect(
178-
function debugDOMHTMLDimensions() {
179-
webshellDebug &&
180-
console.info(
181-
`${
182-
useAutoheight.name
183-
}: DOMHTMLDimensions event #${++numberOfEvents} (implementation: ${implementation}, content width: ${width}, content height: ${height})`
184-
);
185-
},
186-
[webshellDebug, implementation, height, width]
187-
);
224+
if (__DEV__) {
225+
}
188226
return { state, setState };
189227
}
190228

@@ -322,10 +360,7 @@ export default function useAutoheight<
322360
webshellDebug,
323361
onDOMHTMLDimensions: handleOnDOMHTMLDimensions,
324362
style: autoHeightStyle,
325-
scalesPageToFit: false,
326-
showsVerticalScrollIndicator: false,
327-
disableScrollViewPanResponder: true,
328-
contentMode: 'mobile'
363+
...overridenWebViewProps
329364
} as AutoheightState<S>['autoheightWebshellProps'],
330365
resizeImplementation: implementation,
331366
contentSize: state.contentSize,

0 commit comments

Comments
 (0)