Skip to content

Commit

Permalink
[@mantine/core] Spoiler: Add aria-expanded and aria-controls attr…
Browse files Browse the repository at this point in the history
…ibutes to show/hide control (#3183)
  • Loading branch information
rtivital committed Sep 21, 2023
1 parent 6cf1409 commit 746a566
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/mantine-core/src/components/Spoiler/Spoiler.module.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.root {
position: relative;
margin-bottom: rem(24px);
}

.content {
Expand All @@ -13,4 +14,5 @@
position: absolute;
left: 0;
top: 100%;
height: rem(24px);
}
3 changes: 2 additions & 1 deletion src/mantine-core/src/components/Spoiler/Spoiler.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ export function Usage() {
luctus. Donec vitae est id velit condimentum mollis id vel est. Sed eleifend interdum enim,
a facilisis ex faucibus nec. Morbi vel est et mauris congue ullamcorper. Duis eget velit
lacinia, consequat neque vel, dignissim massa.
<input />
<input aria-label="test-input" />
</Spoiler>
<div>Some content after the spoiler</div>
</div>
);
}
12 changes: 9 additions & 3 deletions src/mantine-core/src/components/Spoiler/Spoiler.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from 'react';
import { useElementSize } from '@mantine/hooks';
import { useElementSize, useId } from '@mantine/hooks';
import {
Box,
BoxProps,
Expand Down Expand Up @@ -78,6 +78,7 @@ export const Spoiler = factory<SpoilerFactory>((_props, ref) => {
children,
controlRef,
transitionDuration,
id,
...others
} = props;

Expand All @@ -94,6 +95,8 @@ export const Spoiler = factory<SpoilerFactory>((_props, ref) => {
varsResolver,
});

const _id = useId(id);
const regionId = `${_id}-region`;
const [show, setShowState] = useState(initialState);
const { ref: contentRef, height } = useElementSize();
const spoiler = maxHeight! < height;
Expand All @@ -105,18 +108,21 @@ export const Spoiler = factory<SpoilerFactory>((_props, ref) => {
};

return (
<Box {...getStyles('root')} ref={ref} {...others}>
<Box {...getStyles('root')} id={_id} ref={ref} {...others}>
{spoiler && (
<Anchor
component="button"
type="button"
ref={controlRef}
onClick={() => setShowState((opened) => !opened)}
aria-expanded={show}
aria-controls={regionId}
{...getStyles('control')}
>
{spoilerMoreContent}
</Anchor>
)}
<div {...getStyles('content', { style: s })} data-reduce-motion>
<div {...getStyles('content', { style: s })} data-reduce-motion role="region" id={regionId}>
<div ref={contentRef}>{children}</div>
</div>
</Box>
Expand Down

0 comments on commit 746a566

Please sign in to comment.