-
Notifications
You must be signed in to change notification settings - Fork 4
fix(a11y): color contrast, skip link, focus rings, and aria attributes #421
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
base: test/demo-smoke
Are you sure you want to change the base?
Changes from all commits
cf8852d
1ae1991
6a2f37e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import { | |
| type RefObject, | ||
| useEffect, | ||
| useRef, | ||
| useState, | ||
| } from 'react' | ||
| import { formatUnits, maxUint256, parseUnits } from 'viem' | ||
| export type RenderInputProps = Omit<InputProps, 'onChange'> & { | ||
|
|
@@ -66,14 +67,23 @@ export const BigNumberInput: FC<BigNumberInputProps> = ({ | |
| value, | ||
| }: BigNumberInputProps) => { | ||
| const inputRef = useRef<HTMLInputElement>(null) | ||
| const [hasError, setHasError] = useState(false) | ||
|
|
||
| // update inputValue when value changes | ||
| useEffect(() => { | ||
| const current = inputRef.current | ||
| if (!current) { | ||
| return | ||
| } | ||
| const currentInputValue = parseUnits(current.value.replace(/,/g, '') || '0', decimals) | ||
| // The input may contain an intermediate/unparseable string while the user is | ||
| // typing; guard against a parseUnits throw so an external value update never | ||
| // crashes the effect. | ||
| let currentInputValue: bigint | ||
| try { | ||
| currentInputValue = parseUnits(current.value.replace(/,/g, '') || '0', decimals) | ||
| } catch { | ||
| currentInputValue = BigInt(-1) // sentinel: force the DOM value to be overwritten | ||
| } | ||
|
|
||
| if (currentInputValue !== value) { | ||
| current.value = formatUnits(value, decimals) | ||
|
|
@@ -91,6 +101,7 @@ export const BigNumberInput: FC<BigNumberInputProps> = ({ | |
| const { value } = typeof event === 'string' ? { value: event } : event.currentTarget | ||
|
|
||
| if (value === '') { | ||
| setHasError(false) | ||
| onChange(BigInt(0)) | ||
| return | ||
| } | ||
|
|
@@ -125,12 +136,16 @@ export const BigNumberInput: FC<BigNumberInputProps> = ({ | |
| }] and value is: ${value}` | ||
| console.warn(message) | ||
| onError?.({ value, message }) | ||
| setHasError(true) | ||
| } else { | ||
| setHasError(false) | ||
| } | ||
|
Comment on lines
136
to
142
|
||
|
|
||
| onChange(newValue) | ||
| } | ||
|
|
||
| const inputProps = { | ||
| 'aria-invalid': (hasError || undefined) as true | undefined, | ||
| disabled, | ||
| onChange: updateValue, | ||
| placeholder, | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,7 +7,7 @@ import { Toaster } from '@/src/components/ui/toaster' | |||||||
| import { TransactionNotificationProvider } from '@/src/providers/TransactionNotificationProvider' | ||||||||
| import { Web3Provider } from '@/src/providers/Web3Provider' | ||||||||
| import { printAppInfo } from '@/src/utils/printAppInfo' | ||||||||
| import { Flex } from '@chakra-ui/react' | ||||||||
| import { Flex, chakra } from '@chakra-ui/react' | ||||||||
| import { Outlet, createRootRoute } from '@tanstack/react-router' | ||||||||
| import { Analytics } from '@vercel/analytics/react' | ||||||||
| import { useEffect } from 'react' | ||||||||
|
|
@@ -30,11 +30,26 @@ function Root() { | |||||||
| minH="100vh" | ||||||||
| w="100%" | ||||||||
| > | ||||||||
| <chakra.a | ||||||||
| bg="bg.default" | ||||||||
| color="text.default" | ||||||||
| href="#main-content" | ||||||||
| left={0} | ||||||||
| p={2} | ||||||||
| position="absolute" | ||||||||
| top="-100%" | ||||||||
| zIndex="tooltip" | ||||||||
| _focusVisible={{ top: 0 }} | ||||||||
| > | ||||||||
| Skip to main content | ||||||||
| </chakra.a> | ||||||||
| <Header /> | ||||||||
| <Flex | ||||||||
| as="main" | ||||||||
| direction="column" | ||||||||
| flexGrow="1" | ||||||||
| id="main-content" | ||||||||
|
||||||||
| id="main-content" | |
| id="main-content" | |
| tabIndex={-1} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed: added tabIndex={-1} to the <Flex as="main"> element.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the
useEffectthat syncs the DOM input value, theparseUnits(...)call inside the effect can throw if the input currently contains an unparseable intermediate string (possible becauseupdateValuereturns early on parse errors). Since this effect runs on externalvalueupdates, an external update while the user has invalid text could crash rendering; wrap theparseUnitscall in a try/catch or guard the string before parsing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed: wrapped
parseUnitsin a try/catch; on failure a sentinel value ofBigInt(-1)forces the DOM value to be overwritten, ensuring the externalvalueprop always wins when the current input is unparseable.