UNPKG

@mantine/core

Version:

React components library focused on usability, accessibility and developer experience

1 lines 1.84 kB
{"version":3,"file":"FocusTrap.mjs","names":[],"sources":["../../../src/components/FocusTrap/FocusTrap.tsx"],"sourcesContent":["import { cloneElement } from 'react';\nimport { useFocusTrap, useMergedRef } from '@mantine/hooks';\nimport { getSingleElementChild } from '../../core';\nimport { VisuallyHidden } from '../VisuallyHidden';\n\nexport interface FocusTrapProps {\n /** Element to trap focus at, must support ref prop */\n children: any;\n\n /** If set to `false`, disables focus trap */\n active?: boolean;\n\n /** Prop that is used to access element ref @default 'ref' */\n refProp?: string;\n\n /** Ref to combine with the focus trap ref */\n innerRef?: React.Ref<any>;\n}\n\nexport function FocusTrap({\n children,\n active = true,\n refProp = 'ref',\n innerRef,\n}: FocusTrapProps): React.ReactElement {\n const focusTrapRef = useFocusTrap(active);\n const ref = useMergedRef(focusTrapRef, innerRef);\n\n const child = getSingleElementChild(children);\n if (!child) {\n return children;\n }\n\n return cloneElement(child, { [refProp]: ref });\n}\n\nexport function FocusTrapInitialFocus(props: React.ComponentProps<'span'>) {\n return <VisuallyHidden tabIndex={-1} data-autofocus {...props} />;\n}\n\nFocusTrap.displayName = '@mantine/core/FocusTrap';\nFocusTrapInitialFocus.displayName = '@mantine/core/FocusTrapInitialFocus';\nFocusTrap.InitialFocus = FocusTrapInitialFocus;\n\nexport namespace FocusTrap {\n export type Props = FocusTrapProps;\n}\n"],"mappings":";;;;;;;AAmBA,SAAgB,UAAU,EACxB,UACA,SAAS,MACT,UAAU,OACV,YACqC;CAErC,MAAM,MAAM,aADS,aAAa,OAAO,EACF,SAAS;CAEhD,MAAM,QAAQ,sBAAsB,SAAS;AAC7C,KAAI,CAAC,MACH,QAAO;AAGT,QAAO,aAAa,OAAO,GAAG,UAAU,KAAK,CAAC;;AAGhD,SAAgB,sBAAsB,OAAqC;AACzE,QAAO,oBAAC,gBAAD;EAAgB,UAAU;EAAI,kBAAA;EAAe,GAAI;EAAS,CAAA;;AAGnE,UAAU,cAAc;AACxB,sBAAsB,cAAc;AACpC,UAAU,eAAe"}