Skip to content

docs: clear up relative position confusion #8102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/@react-aria/visually-hidden/docs/VisuallyHidden.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,17 @@ let {visuallyHiddenProps} = useVisuallyHidden();

<div {...visuallyHiddenProps}>I am hidden</div>
```

## Gotchas
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better section title? use inline alert?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caveats?


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.

```tsx rendered=false
<label style={{position: 'relative'}}>
<VisuallyHidden>
<input type="checkbox" />
</VisuallyHidden>
<span>Subscribe to our newsletter</span>
</label>
```

2 changes: 2 additions & 0 deletions packages/react-aria-components/docs/Checkbox.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ import {Checkbox} from 'react-aria-components';
--checkmark-color: var(--highlight-foreground);

display: flex;
/* This is needed so the HiddenInput is positioned correctly */
position: relative;
align-items: center;
gap: 0.571rem;
font-size: 1.143rem;
Expand Down
2 changes: 2 additions & 0 deletions packages/react-aria-components/docs/RadioGroup.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ import {RadioGroup, Radio, Label} from 'react-aria-components';

.react-aria-Radio {
display: flex;
/* This is needed so the HiddenInput is positioned correctly */
position: relative;
align-items: center;
gap: 0.571rem;
font-size: 1.143rem;
Expand Down
2 changes: 2 additions & 0 deletions packages/react-aria-components/docs/Switch.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ import {Switch} from 'react-aria-components';

.react-aria-Switch {
display: flex;
/* This is needed so the HiddenInput is positioned correctly */
position: relative;
align-items: center;
gap: 0.571rem;
font-size: 1.143rem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function MyMenuItem(props: MenuItemProps) {

function MySwitch(props: SwitchProps) {
return (
<Switch className="group flex gap-2 items-center text-gray-800 dark:text-zinc-200 text-base transition">
<Switch className="group relative flex gap-2 items-center text-gray-800 dark:text-zinc-200 text-base transition">
{composeRenderProps(props.children, children => <>
<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">
<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%]" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import './tailwind.global.css';
import {Switch} from 'react-aria-components';

<div className="bg-linear-to-r from-yellow-300 to-orange-300 p-12 rounded-lg flex justify-center">
<Switch className="group flex gap-2 items-center text-black font-semibold text-lg">
<Switch className="group relative flex gap-2 items-center text-black font-semibold text-lg">
<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">
<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%]" />
</div>
Expand Down
2 changes: 1 addition & 1 deletion starters/tailwind/src/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function CheckboxGroup(props: CheckboxGroupProps) {
}

const checkboxStyles = tv({
base: 'flex gap-2 items-center group text-sm transition',
base: 'flex gap-2 items-center group text-sm transition relative',
variants: {
isDisabled: {
false: 'text-gray-800 dark:text-zinc-200',
Expand Down
2 changes: 1 addition & 1 deletion starters/tailwind/src/RadioGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const styles = tv({

export function Radio(props: RadioProps) {
return (
<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')}>
<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')}>
{renderProps => <>
<div className={styles(renderProps)} />
{props.children}
Expand Down
2 changes: 1 addition & 1 deletion starters/tailwind/src/Switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const handle = tv({

export function Switch({ children, ...props }: SwitchProps) {
return (
<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')}>
<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')}>
{(renderProps) => (
<>
<div className={track(renderProps)}>
Expand Down