Skip to content
Closed
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
74 changes: 72 additions & 2 deletions packages/react/src/Button/Button.features.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {EyeIcon, TriangleDownIcon, HeartIcon} from '@primer/octicons-react'
import React, {useState} from 'react'
import React, {useState, useRef} from 'react'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
import React, {useState, useRef} from 'react'
import React, {useRef} from 'react'

import {Button} from '.'
import {announce} from '@primer/live-region-element'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
import {announce} from '@primer/live-region-element'
import {announce} from '@primer/live-region-element
test test

import VisuallyHidden from '../_VisuallyHidden'

export default {
title: 'Components/Button/Features',
Expand All @@ -16,7 +18,7 @@ export const LeadingVisual = () => <Button leadingVisual={HeartIcon}>Leading vis

export const TrailingVisual = () => <Button trailingVisual={EyeIcon}>Trailing visual</Button>

export const TrailingCounter = () => {
export const TrailingButtonNothing = () => {
const [count, setCount] = useState(0)
return (
<Button onClick={() => setCount(count + 1)} count={count}>
Expand All @@ -25,6 +27,74 @@ export const TrailingCounter = () => {
)
}

export const TrailingCounterWithAriaLabel = () => {
const [count, setCount] = useState(0)
return (
<Button aria-label={`Watch (${count})`} onClick={() => setCount(count + 1)} count={count}>
Watch
</Button>
)
}

export const TrailingCounterWithFocusBouncing = () => {
const [count, setCount] = useState(0)
const onClick = () => {
setCount(count + 1)
// i think the proper react way is to pass a ref??
const button = document.querySelector('#test-1') as HTMLElement
// eslint-disable-next-line github/no-blur
button.blur()
setTimeout(() => {
button.focus()
}, 250)
}
return (
<div>
<Button id="test-1" onClick={onClick} count={count}>
Watch
</Button>
</div>
)
}

export const TrailingCounterWithPoliteLiveRegion = () => {
const [count, setCount] = useState(0)
return (
<Button aria-live="polite" aria-atomic="true" onClick={() => setCount(count + 1)} count={count}>
Watch
</Button>
)
}

export const TrailingCounterWithAdjacentLiveRegion = () => {
const [count, setCount] = useState(0)

const onClick = () => {
setCount(count + 1)
announce(`Watch (${count + 1})`)
}
return (
<div>
<Button onClick={onClick}>Watch</Button>
</div>
)
}

export const TrailingCounterWithNestedLiveRegion = () => {
const [count, setCount] = useState(0)

const onClick = () => {
setCount(count + 1)
}
return (
<div>
<Button dynamicallyUpdated={true} onClick={onClick} count={count}>
Watch
</Button>
</div>
)
}

export const TrailingCounterAllVariants = () => {
const [count, setCount] = useState(0)
return (
Expand Down
10 changes: 8 additions & 2 deletions packages/react/src/Button/ButtonBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const ButtonBase = forwardRef(
trailingVisual: TrailingVisual,
trailingAction: TrailingAction,
count,
dynamicallyUpdated,
icon: Icon,
variant = 'default',
size = 'medium',
Expand Down Expand Up @@ -62,7 +63,6 @@ const ButtonBase = forwardRef(
}
}, [innerRef])
}

return (
<StyledButton
as={Component}
Expand All @@ -78,7 +78,13 @@ const ButtonBase = forwardRef(
<Icon />
) : (
<>
<Box as="span" data-component="buttonContent" sx={getAlignContentSize(alignContent)}>
<Box
as="span"
data-component="buttonContent"
sx={getAlignContentSize(alignContent)}
aria-live={dynamicallyUpdated ? 'polite' : undefined}
aria-atomic={dynamicallyUpdated ? 'true' : undefined}
>
{LeadingVisual && (
<Box as="span" data-component="leadingVisual" sx={{...iconWrapStyles}}>
<LeadingVisual />
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/Button/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export type ButtonProps = {
trailingAction?: React.ElementType | null

children?: React.ReactNode

dynamicallyUpdated?: boolean
count?: number
} & ButtonBaseProps

Expand Down