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(resize-handle): make ref callback stable #214

Merged
merged 2 commits into from
Aug 14, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: make ref callback stable",
"packageName": "@fluentui-contrib/react-resize-handle",
"email": "[email protected]",
"dependentChangeType": "patch"
}
24 changes: 15 additions & 9 deletions packages/react-resize-handle/src/hooks/useResizeHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export type UseResizeHandleParams = {
* A function that will be called to get the value that will be set as the aria-valuetext attribute on the resize handle.
* Use this for localization.
*/
getA11ValueText?: (value: number) => string;
getA11ValueText?: (value: number) => string | undefined;
Copy link
Member Author

Choose a reason for hiding this comment

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

This actually allows undefined, the default impl also returns undefined.


/**
* Only measure relative change in size, useful to use with CSS calc() function.
Expand All @@ -59,6 +59,10 @@ export type UseResizeHandleParams = {
relative?: boolean;
};

const DEFAULT_GET_A11_VALUE_TEXT: UseResizeHandleParams['getA11ValueText'] = (
value
) => (value === UNMEASURED ? undefined : `${value.toFixed(0)}px`);

export const useResizeHandle = (params: UseResizeHandleParams) => {
const {
growDirection,
Expand All @@ -68,8 +72,7 @@ export const useResizeHandle = (params: UseResizeHandleParams) => {
onChange,
onDragStart,
onDragEnd,
getA11ValueText = (value) =>
value === UNMEASURED ? undefined : `${value.toFixed(0)}px`,
getA11ValueText = DEFAULT_GET_A11_VALUE_TEXT,
Copy link
Member Author

Choose a reason for hiding this comment

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

getA11ValueText before was unstable reference

relative,
} = params;

Expand All @@ -78,6 +81,11 @@ export const useResizeHandle = (params: UseResizeHandleParams) => {
const elementRef = React.useRef<HTMLElement | null>(null);

const currentValue = React.useRef(UNMEASURED);
const handleChange: UseResizeHandleParams['onChange'] = useEventCallback(
(value) => {
onChange?.(value);
}
);
Comment on lines +84 to +88
Copy link
Member Author

Choose a reason for hiding this comment

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

We need to use useEventCallback() callback there, otherwise we make memoization an implicit contract


const updateElementsAttrs = React.useCallback(() => {
const a11yValue = relative
Expand All @@ -90,10 +98,8 @@ export const useResizeHandle = (params: UseResizeHandleParams) => {
tabIndex: 0,
role: 'separator',
'aria-valuemin': minValue,
...(maxValue < Number.MAX_SAFE_INTEGER
? { 'aria-valuemax': maxValue }
: {}),
...(a11yValue ? { 'aria-valuetext': a11yValue } : {}),
...(maxValue < Number.MAX_SAFE_INTEGER && { 'aria-valuemax': maxValue }),
...(a11yValue && { 'aria-valuetext': a11yValue }),
'aria-orientation':
growDirection === 'end' || growDirection === 'start'
? 'vertical'
Expand All @@ -110,9 +116,9 @@ export const useResizeHandle = (params: UseResizeHandleParams) => {
variableName,
`${currentValue.current}px`
);
onChange?.(currentValue.current);
handleChange(currentValue.current);
}
}, [getA11ValueText, maxValue, minValue, onChange, variableName]);
}, [getA11ValueText, maxValue, minValue, handleChange, variableName]);

// In case the maxValue or minValue is changed, we need to make sure we are not exceeding the new limits
React.useEffect(() => {
Expand Down