address feedback
This commit is contained in:
parent
64537813c3
commit
aa6064e676
|
|
@ -56,13 +56,8 @@ export const AutoChip: React.FC<{
|
|||
anchorId?: string,
|
||||
}> = ({ header, initialExpanded, noInsets, children, dataTestId, anchorId }) => {
|
||||
const [expanded, setExpanded] = React.useState(initialExpanded ?? true);
|
||||
const onChangeReveal = React.useCallback((isRevealed: boolean) => {
|
||||
if (isRevealed)
|
||||
setExpanded(true);
|
||||
}, [setExpanded]);
|
||||
return <Anchor
|
||||
id={anchorId}
|
||||
onChange={onChangeReveal}>
|
||||
const onReveal = React.useCallback(() => setExpanded(true), []);
|
||||
return <Anchor id={anchorId} onReveal={onReveal}>
|
||||
<Chip
|
||||
header={header}
|
||||
expanded={expanded}
|
||||
|
|
|
|||
|
|
@ -116,33 +116,27 @@ const kMissingContentType = 'x-playwright/missing';
|
|||
|
||||
type AnchorID = string | ((id: string | null) => boolean) | undefined;
|
||||
|
||||
export function useAnchor(id: AnchorID, onChange: (isRevealed: boolean) => void) {
|
||||
function useAnchor(id: AnchorID, onReveal: () => void) {
|
||||
React.useEffect(() => {
|
||||
const listener = () => {
|
||||
const params = new URLSearchParams(window.location.hash.slice(1));
|
||||
const anchor = params.get('anchor');
|
||||
const isRevealed = typeof id === 'function' ? id(anchor) : anchor === id;
|
||||
onChange(isRevealed);
|
||||
if (isRevealed)
|
||||
onReveal();
|
||||
};
|
||||
window.addEventListener('popstate', listener);
|
||||
return () => window.removeEventListener('popstate', listener);
|
||||
}, [id, onChange]);
|
||||
}, [id, onReveal]);
|
||||
}
|
||||
|
||||
export function Anchor({ id, onChange, children }: React.PropsWithChildren<{ id: AnchorID, onChange?(isRevealed: boolean, ref: HTMLDivElement): void }>) {
|
||||
export function Anchor({ id, onReveal, children }: React.PropsWithChildren<{ id: AnchorID, onReveal?(): void }>) {
|
||||
const ref = React.useRef<HTMLDivElement>(null);
|
||||
const onAnchorChange = React.useCallback((isRevealed: boolean) => {
|
||||
if (!ref.current)
|
||||
return;
|
||||
|
||||
const preventDefault = onChange?.(isRevealed, ref.current);
|
||||
if (preventDefault)
|
||||
return;
|
||||
|
||||
if (isRevealed)
|
||||
const onAnchorReveal = React.useCallback(() => {
|
||||
onReveal?.();
|
||||
requestAnimationFrame(() => ref.current?.scrollIntoView({ block: 'start', inline: 'start' }));
|
||||
}, [onChange]);
|
||||
useAnchor(id, onAnchorChange);
|
||||
}, [onReveal]);
|
||||
useAnchor(id, onAnchorReveal);
|
||||
|
||||
return <div ref={ref}>{children}</div>;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue