|
1 | 1 | 'use client'; |
2 | | -import React from 'react'; |
| 2 | +import React, { CSSProperties } from 'react'; |
3 | 3 | import Primitive from '~/core/primitives/Primitive'; |
4 | 4 | import { customClassSwitcher } from '~/core'; |
5 | 5 | import { clsx } from 'clsx'; |
| 6 | + |
6 | 7 | const COMPONENT_NAME = 'VisuallyHidden'; |
7 | 8 |
|
8 | 9 | export type VisuallyHiddenProps = { |
9 | 10 | children: React.ReactNode; |
10 | 11 | customRootClass?: string; |
11 | 12 | className?: string; |
12 | 13 | asChild?: boolean; |
13 | | - props: Record<string, any>[]; |
14 | | -} |
| 14 | + style?: CSSProperties; |
| 15 | +} & React.HTMLAttributes<HTMLDivElement>; |
| 16 | + |
| 17 | +const VISUALLY_HIDDEN_STYLES: CSSProperties = { |
| 18 | + position: 'absolute', |
| 19 | + width: '1px', |
| 20 | + height: '1px', |
| 21 | + margin: '-1px', |
| 22 | + border: '0', |
| 23 | + padding: '0', |
| 24 | + whiteSpace: 'nowrap', |
| 25 | + clip: 'rect(0 0 0 0)', |
| 26 | + clipPath: 'inset(50%)', |
| 27 | + overflow: 'hidden', |
| 28 | + pointerEvents: 'none', |
| 29 | + userSelect: 'none', |
| 30 | + visibility: 'hidden' |
| 31 | +} as const; |
15 | 32 |
|
16 | | -const VisuallyHidden = ({ children, customRootClass, className, ...props }: VisuallyHiddenProps) => { |
| 33 | +const VisuallyHidden = ({ |
| 34 | + children, |
| 35 | + customRootClass, |
| 36 | + className, |
| 37 | + style = {}, |
| 38 | + ...props |
| 39 | +}: VisuallyHiddenProps) => { |
17 | 40 | const rootClass = customClassSwitcher(customRootClass, COMPONENT_NAME); |
18 | | - return <Primitive.div className={clsx(rootClass, className)} {...props}>{children}</Primitive.div>; |
| 41 | + |
| 42 | + return ( |
| 43 | + <Primitive.div |
| 44 | + className={clsx(rootClass, className)} |
| 45 | + style={{ ...VISUALLY_HIDDEN_STYLES, ...style }} // overriding possible |
| 46 | + {...props} |
| 47 | + > |
| 48 | + {children} |
| 49 | + </Primitive.div> |
| 50 | + ); |
19 | 51 | }; |
| 52 | + |
20 | 53 | VisuallyHidden.displayName = COMPONENT_NAME; |
21 | 54 |
|
22 | 55 | export default VisuallyHidden; |
0 commit comments