Skip to content

Commit 609739b

Browse files
docs: clear up relative position confusion (#8102)
* docs: clear up relative position cofusion * Update packages/@react-aria/visually-hidden/docs/VisuallyHidden.mdx Co-authored-by: Yihui Liao <44729383+yihuiliao@users.noreply.github.com> --------- Co-authored-by: Yihui Liao <44729383+yihuiliao@users.noreply.github.com>
1 parent 8f489a2 commit 609739b

File tree

9 files changed

+25
-5
lines changed

9 files changed

+25
-5
lines changed

packages/@react-aria/visually-hidden/docs/VisuallyHidden.mdx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,17 @@ let {visuallyHiddenProps} = useVisuallyHidden();
5959

6060
<div {...visuallyHiddenProps}>I am hidden</div>
6161
```
62+
63+
## Gotchas
64+
65+
VisuallyHidden is positioned absolutely, so it needs a positioned ancestor. Otherwise, it will be positioned relative to the nearest positioned ancestor, which may be the body, causing undesired scroll bars to appear.
66+
67+
```tsx rendered=false
68+
<label style={{position: 'relative'}}>
69+
<VisuallyHidden>
70+
<input type="checkbox" />
71+
</VisuallyHidden>
72+
<span>Subscribe to our newsletter</span>
73+
</label>
74+
```
75+

packages/react-aria-components/docs/Checkbox.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ import {Checkbox} from 'react-aria-components';
6666
--checkmark-color: var(--highlight-foreground);
6767

6868
display: flex;
69+
/* This is needed so the HiddenInput is positioned correctly */
70+
position: relative;
6971
align-items: center;
7072
gap: 0.571rem;
7173
font-size: 1.143rem;

packages/react-aria-components/docs/RadioGroup.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ import {RadioGroup, Radio, Label} from 'react-aria-components';
7676

7777
.react-aria-Radio {
7878
display: flex;
79+
/* This is needed so the HiddenInput is positioned correctly */
80+
position: relative;
7981
align-items: center;
8082
gap: 0.571rem;
8183
font-size: 1.143rem;

packages/react-aria-components/docs/Switch.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ import {Switch} from 'react-aria-components';
5959

6060
.react-aria-Switch {
6161
display: flex;
62+
/* This is needed so the HiddenInput is positioned correctly */
63+
position: relative;
6264
align-items: center;
6365
gap: 0.571rem;
6466
font-size: 1.143rem;

packages/react-aria-components/docs/examples/account-menu.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function MyMenuItem(props: MenuItemProps) {
8080

8181
function MySwitch(props: SwitchProps) {
8282
return (
83-
<Switch className="group flex gap-2 items-center text-gray-800 dark:text-zinc-200 text-base transition">
83+
<Switch className="group relative flex gap-2 items-center text-gray-800 dark:text-zinc-200 text-base transition">
8484
{composeRenderProps(props.children, children => <>
8585
<div className="flex h-3 w-6 p-[2px] items-center shrink-0 cursor-default rounded-full transition duration-200 ease-in-out shadow-inner border border-transparent bg-gray-400 dark:bg-zinc-400 group-pressed:bg-gray-500 dark:group-pressed:bg-zinc-300 group-selected:bg-gray-700 group-selected:dark:bg-zinc-300 group-selected:forced-colors:bg-[Highlight]! group-selected:group-pressed:bg-gray-800 group-selected:dark:group-pressed:bg-zinc-200 outline outline-0 outline-blue-600 dark:outline-blue-500 forced-colors:outline-[Highlight] outline-offset-2 group-focus-visible:outline-2">
8686
<div className="h-3 w-3 transform rounded-full bg-white dark:bg-zinc-900 outline outline-1 -outline-offset-1 outline-transparent shadow-sm transition duration-200 ease-in-out translate-x-0 group-selected:translate-x-[100%]" />

packages/react-aria-components/docs/examples/wifi-switch.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import './tailwind.global.css';
3838
import {Switch} from 'react-aria-components';
3939

4040
<div className="bg-linear-to-r from-yellow-300 to-orange-300 p-12 rounded-lg flex justify-center">
41-
<Switch className="group flex gap-2 items-center text-black font-semibold text-lg">
41+
<Switch className="group relative flex gap-2 items-center text-black font-semibold text-lg">
4242
<div className="flex h-[26px] w-[44px] shrink-0 cursor-default rounded-full shadow-inner bg-clip-padding border border-solid border-white/30 p-[3px] box-border transition duration-200 ease-in-out bg-yellow-600 group-pressed:bg-yellow-700 group-selected:bg-amber-800 group-selected:group-pressed:bg-amber-900 outline-hidden group-focus-visible:ring-2 ring-black">
4343
<span className="h-[18px] w-[18px] transform rounded-full bg-white shadow-sm transition duration-200 ease-in-out translate-x-0 group-selected:translate-x-[100%]" />
4444
</div>

starters/tailwind/src/Checkbox.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function CheckboxGroup(props: CheckboxGroupProps) {
2424
}
2525

2626
const checkboxStyles = tv({
27-
base: 'flex gap-2 items-center group text-sm transition',
27+
base: 'flex gap-2 items-center group text-sm transition relative',
2828
variants: {
2929
isDisabled: {
3030
false: 'text-gray-800 dark:text-zinc-200',

starters/tailwind/src/RadioGroup.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const styles = tv({
4343

4444
export function Radio(props: RadioProps) {
4545
return (
46-
<RACRadio {...props} className={composeTailwindRenderProps(props.className, 'flex gap-2 items-center group text-gray-800 disabled:text-gray-300 dark:text-zinc-200 dark:disabled:text-zinc-600 forced-colors:disabled:text-[GrayText] text-sm transition')}>
46+
<RACRadio {...props} className={composeTailwindRenderProps(props.className, 'flex relative gap-2 items-center group text-gray-800 disabled:text-gray-300 dark:text-zinc-200 dark:disabled:text-zinc-600 forced-colors:disabled:text-[GrayText] text-sm transition')}>
4747
{renderProps => <>
4848
<div className={styles(renderProps)} />
4949
{props.children}

starters/tailwind/src/Switch.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const handle = tv({
3939

4040
export function Switch({ children, ...props }: SwitchProps) {
4141
return (
42-
<AriaSwitch {...props} className={composeTailwindRenderProps(props.className, 'group flex gap-2 items-center text-gray-800 disabled:text-gray-300 dark:text-zinc-200 dark:disabled:text-zinc-600 forced-colors:disabled:text-[GrayText] text-sm transition')}>
42+
<AriaSwitch {...props} className={composeTailwindRenderProps(props.className, 'group relative flex gap-2 items-center text-gray-800 disabled:text-gray-300 dark:text-zinc-200 dark:disabled:text-zinc-600 forced-colors:disabled:text-[GrayText] text-sm transition')}>
4343
{(renderProps) => (
4444
<>
4545
<div className={track(renderProps)}>

0 commit comments

Comments
 (0)