Skip to content
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

fix: Spinbutton aria-valuenow vs native value update timing fix #33923

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
fix: Spinbutton aria-valuenow vs native value timing fix
  • Loading branch information
smhigley committed Feb 28, 2025
commit 9ab71668f54c3efdf2c0953bcafe9e0077aeceaf
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import {
useControllableState,
useTimeout,
slot,
useMergedRefs,
} from '@fluentui/react-utilities';
import { ArrowUp, ArrowDown, End, Enter, Escape, Home, PageDown, PageUp } from '@fluentui/keyboard-keys';
import {
@@ -86,6 +87,8 @@ export const useSpinButton_unstable = (props: SpinButtonProps, ref: React.Ref<HT
initialState: 0,
});

const inputRef = React.useRef<HTMLInputElement>(null);

const isControlled = value !== undefined;

const [textValue, setTextValue] = React.useState<string | undefined>(undefined);
@@ -233,12 +236,21 @@ export const useSpinButton_unstable = (props: SpinButtonProps, ref: React.Ref<HT
if (valueChanged) {
roundedValue = precisionRound(newValue!, precision);
setCurrentValue(roundedValue);
if (inputRef.current) {
// we need to set this here using the IDL attribute directly, because otherwise the timing of the ARIA value update
// is not in sync with the user-entered native input value, and some screen readers end up reading the previous value.
inputRef.current.ariaValueNow = `${roundedValue}`;
}
internalState.current.value = roundedValue;
} else if (displayValueChanged && !isControlled) {
const nextValue = parseFloat(newDisplayValue as string);
if (!isNaN(nextValue)) {
setCurrentValue(precisionRound(nextValue, precision));
internalState.current.value = precisionRound(nextValue, precision);
const roundedNextValue = precisionRound(nextValue, precision);
setCurrentValue(roundedNextValue);
if (inputRef.current) {
inputRef.current.ariaValueNow = `${roundedNextValue}`;
}
internalState.current.value = roundedNextValue;
}
}

@@ -285,7 +297,6 @@ export const useSpinButton_unstable = (props: SpinButtonProps, ref: React.Ref<HT
}),
input: slot.always(input, {
defaultProps: {
ref,
autoComplete: 'off',
role: 'spinbutton',
appearance,
@@ -323,6 +334,7 @@ export const useSpinButton_unstable = (props: SpinButtonProps, ref: React.Ref<HT
};

state.input.value = valueToDisplay;
state.input.ref = useMergedRefs(inputRef, ref);
state.input['aria-valuemin'] = min;
state.input['aria-valuemax'] = max;
state.input['aria-valuetext'] = state.input['aria-valuetext'] ?? ((value !== undefined && displayValue) || undefined);