UNPKG

@wordpress/components

Version:
30 lines (29 loc) • 1.03 kB
import { useCallback } from "@wordpress/element"; import { escapeAttribute } from "@wordpress/escape-html"; import { useContextSystem } from "../../context/index.mjs"; import Button from "../../button/index.mjs"; import { useNavigator } from "../use-navigator.mjs"; // packages/components/src/navigator/navigator-button/hook.ts const cssSelectorForAttribute = (attrName, attrValue) => `[${attrName}="${attrValue}"]`; function useNavigatorButton(props) { const { path, onClick, as = Button, attributeName = "id", ...otherProps } = useContextSystem(props, "Navigator.Button"); const escapedPath = escapeAttribute(path); const { goTo } = useNavigator(); return { as, onClick: useCallback((e) => { e.preventDefault(); goTo(escapedPath, { focusTargetSelector: cssSelectorForAttribute(attributeName, escapedPath) }); onClick?.(e); }, [ goTo, onClick, attributeName, escapedPath ]), ...otherProps, [attributeName]: escapedPath }; } export { useNavigatorButton }; //# sourceMappingURL=hook.mjs.map