UNPKG

@wordpress/components

Version:
8 lines (7 loc) 6.58 kB
{ "version": 3, "sources": ["../../../src/navigator/navigator-screen/component.tsx"], "sourcesContent": ["/**\n * External dependencies\n */\n\n/**\n * WordPress dependencies\n */\nimport { focus } from '@wordpress/dom';\nimport { useContext, useEffect, useMemo, useRef, useId } from '@wordpress/element';\nimport { useMergeRefs } from '@wordpress/compose';\nimport { escapeAttribute } from '@wordpress/escape-html';\nimport warning from '@wordpress/warning';\n\n/**\n * Internal dependencies\n */\n\nimport { contextConnect, useContextSystem } from '../../context';\nimport { useCx } from '../../utils/hooks/use-cx';\nimport { View } from '../../view';\nimport { NavigatorContext } from '../context';\nimport * as styles from '../styles';\nimport { useScreenAnimatePresence } from './use-screen-animate-presence';\nimport { jsx as _jsx } from \"react/jsx-runtime\";\nfunction UnconnectedNavigatorScreen(props, forwardedRef) {\n if (!/^\\//.test(props.path)) {\n globalThis.SCRIPT_DEBUG === true ? warning('wp.components.Navigator.Screen: the `path` should follow a URL-like scheme; it should start with and be separated by the `/` character.') : void 0;\n }\n const screenId = useId();\n const {\n children,\n className,\n path,\n onAnimationEnd: onAnimationEndProp,\n ...otherProps\n } = useContextSystem(props, 'Navigator.Screen');\n const {\n location,\n match,\n addScreen,\n removeScreen\n } = useContext(NavigatorContext);\n const {\n isInitial,\n isBack,\n focusTargetSelector,\n skipFocus\n } = location;\n const isMatch = match === screenId;\n const wrapperRef = useRef(null);\n const skipAnimationAndFocusRestoration = !!isInitial && !isBack;\n\n // Register / unregister screen with the navigator context.\n useEffect(() => {\n const screen = {\n id: screenId,\n path: escapeAttribute(path)\n };\n addScreen(screen);\n return () => removeScreen(screen);\n }, [screenId, path, addScreen, removeScreen]);\n\n // Animation.\n const {\n animationStyles,\n shouldRenderScreen,\n screenProps\n } = useScreenAnimatePresence({\n isMatch,\n isBack,\n onAnimationEnd: onAnimationEndProp,\n skipAnimation: skipAnimationAndFocusRestoration\n });\n const cx = useCx();\n const classes = useMemo(() => cx(styles.navigatorScreen, animationStyles, className), [className, cx, animationStyles]);\n\n // Focus restoration\n const locationRef = useRef(location);\n useEffect(() => {\n locationRef.current = location;\n }, [location]);\n useEffect(() => {\n const wrapperEl = wrapperRef.current;\n // Only attempt to restore focus:\n // - if the current location is not the initial one (to avoid moving focus on page load)\n // - when the screen becomes visible\n // - if the wrapper ref has been assigned\n // - if focus hasn't already been restored for the current location\n // - if the `skipFocus` option is not set to `true`. This is useful when we trigger the navigation outside of NavigatorScreen.\n if (skipAnimationAndFocusRestoration || !isMatch || !wrapperEl || locationRef.current.hasRestoredFocus || skipFocus) {\n return;\n }\n const activeElement = wrapperEl.ownerDocument.activeElement;\n\n // If an element is already focused within the wrapper do not focus the\n // element. This prevents inputs or buttons from losing focus unnecessarily.\n if (wrapperEl.contains(activeElement)) {\n return;\n }\n let elementToFocus = null;\n\n // When navigating back, if a selector is provided, use it to look for the\n // target element (assumed to be a node inside the current NavigatorScreen)\n if (isBack && focusTargetSelector) {\n elementToFocus = wrapperEl.querySelector(focusTargetSelector);\n }\n\n // If the previous query didn't run or find any element to focus, fallback\n // to the first tabbable element in the screen (or the screen itself).\n if (!elementToFocus) {\n const [firstTabbable] = focus.tabbable.find(wrapperEl);\n elementToFocus = firstTabbable ?? wrapperEl;\n }\n locationRef.current.hasRestoredFocus = true;\n elementToFocus.focus();\n }, [skipAnimationAndFocusRestoration, isMatch, isBack, focusTargetSelector, skipFocus]);\n const mergedWrapperRef = useMergeRefs([forwardedRef, wrapperRef]);\n return shouldRenderScreen ? /*#__PURE__*/_jsx(View, {\n ref: mergedWrapperRef,\n className: classes,\n ...screenProps,\n ...otherProps,\n children: children\n }) : null;\n}\nexport const NavigatorScreen = contextConnect(UnconnectedNavigatorScreen, 'Navigator.Screen');"], "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAOA,iBAAsB;AACtB,qBAA8D;AAC9D,qBAA6B;AAC7B,yBAAgC;AAChC,qBAAoB;AAMpB,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