-
Notifications
You must be signed in to change notification settings - Fork 1
feat(Resizable): support single-axis sizing (DS-5552) #483
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
Changes from all commits
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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { createRef, type ComponentProps } from 'react'; | ||
| import { createRef, useState, type ComponentProps } from 'react'; | ||
|
|
||
| import type * as ReactCore from '@koobiq/react-core'; | ||
| import { fireEvent, render, screen } from '@testing-library/react'; | ||
|
|
@@ -70,33 +70,6 @@ describe('Resizable', () => { | |
| beforeEach(() => { | ||
| mocks.observedSize = { width: 300, height: 200 }; | ||
| vi.stubGlobal('PointerEvent', undefined); | ||
|
|
||
| vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation( | ||
| function getBoundingClientRect(this: HTMLElement) { | ||
| const width = Number.parseFloat(this.style.width); | ||
| const height = Number.parseFloat(this.style.height); | ||
|
|
||
| const resolvedWidth = Number.isNaN(width) | ||
| ? mocks.observedSize.width | ||
| : width; | ||
|
|
||
| const resolvedHeight = Number.isNaN(height) | ||
| ? mocks.observedSize.height | ||
| : height; | ||
|
|
||
| return { | ||
| x: 0, | ||
| y: 0, | ||
| top: 0, | ||
| left: 0, | ||
| right: resolvedWidth, | ||
| bottom: resolvedHeight, | ||
| width: resolvedWidth, | ||
| height: resolvedHeight, | ||
| toJSON: () => ({}), | ||
| }; | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
|
|
@@ -280,7 +253,110 @@ describe('Resizable', () => { | |
|
|
||
| fireEvent.keyDown(getHandle(), { key: 'ArrowRight' }); | ||
|
|
||
| expect(getRoot()).toHaveStyle({ width: '261px', height: '140px' }); | ||
| // Only the resized axis becomes managed, the other one is left to CSS. | ||
| expect(getRoot()).toHaveStyle({ width: '261px' }); | ||
| expect(getRoot().style.height).toBe(''); | ||
| }); | ||
|
|
||
| it('manages a single dimension when the size defines one axis', () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. test-coverage — every new single-axis test is uncontrolled. Nothing here exercises A test asserting
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||
| const onResize = vi.fn(); | ||
|
|
||
| mocks.observedSize = { width: 300, height: 200 }; | ||
|
|
||
| renderResizable([1, 0], { | ||
| defaultSize: { width: 300 }, | ||
| minSize: { width: 100 }, | ||
| maxSize: { width: 500 }, | ||
| onResize, | ||
| }); | ||
|
|
||
| expect(getRoot()).toHaveStyle({ | ||
| width: '300px', | ||
| minWidth: '100px', | ||
| maxWidth: '500px', | ||
| }); | ||
|
|
||
| expect(getRoot().style.height).toBe(''); | ||
| expect(getRoot().style.minHeight).toBe(''); | ||
| expect(getRoot().style.maxHeight).toBe(''); | ||
|
|
||
| drag(25, 40); | ||
|
|
||
| // The callbacks still report both dimensions, measured where unmanaged. | ||
| expect(onResize).toHaveBeenLastCalledWith({ width: 325, height: 200 }); | ||
| expect(getRoot()).toHaveStyle({ width: '325px' }); | ||
| expect(getRoot().style.height).toBe(''); | ||
| }); | ||
|
|
||
| it('keeps the omitted axis unmanaged in controlled state', () => { | ||
| function ControlledWidth() { | ||
| const [width, setWidth] = useState(300); | ||
|
|
||
| return ( | ||
| <Resizable | ||
| data-testid="resizable" | ||
| size={{ width }} | ||
| onResize={(size) => setWidth(size.width)} | ||
| > | ||
| <Resizable.Handle data-testid="handle" direction={[1, 0]} /> | ||
| </Resizable> | ||
| ); | ||
| } | ||
|
|
||
| render(<ControlledWidth />); | ||
| drag(20, 10); | ||
|
|
||
| expect(getRoot()).toHaveStyle({ width: '320px' }); | ||
| expect(getRoot().style.height).toBe(''); | ||
| }); | ||
|
|
||
| it('keeps the managed axis when another axis is resized', () => { | ||
| render( | ||
| <Resizable data-testid="resizable" defaultSize={{ width: 300 }}> | ||
| <Resizable.Handle data-testid="handle" direction={[0, 1]} /> | ||
| </Resizable> | ||
| ); | ||
|
|
||
| fireEvent.keyDown(getHandle(), { key: 'ArrowDown' }); | ||
|
|
||
| expect(getRoot()).toHaveStyle({ width: '300px', height: '201px' }); | ||
| }); | ||
|
|
||
| it('disables arrow key resizing without breaking dragging', () => { | ||
| render( | ||
| <Resizable data-testid="resizable" defaultSize={{ width: 300 }}> | ||
| <Resizable.Handle | ||
| data-testid="handle" | ||
| direction={[1, 0]} | ||
| disableKeyboardResize | ||
| /> | ||
| </Resizable> | ||
| ); | ||
|
|
||
| expect(getHandle()).toHaveAttribute('tabindex', '-1'); | ||
|
|
||
| fireEvent.keyDown(getHandle(), { key: 'ArrowRight' }); | ||
|
|
||
| expect(getRoot()).toHaveStyle({ width: '300px' }); | ||
|
|
||
| drag(20, 0); | ||
|
|
||
| expect(getRoot()).toHaveStyle({ width: '320px' }); | ||
| }); | ||
|
|
||
| it('keeps the handle focusable when tabIndex is set explicitly', () => { | ||
| render( | ||
| <Resizable data-testid="resizable" defaultSize={{ width: 300 }}> | ||
| <Resizable.Handle | ||
| data-testid="handle" | ||
| direction={[1, 0]} | ||
| tabIndex={0} | ||
| disableKeyboardResize | ||
| /> | ||
| </Resizable> | ||
| ); | ||
|
|
||
| expect(getHandle()).toHaveAttribute('tabindex', '0'); | ||
| }); | ||
|
|
||
| it('calls lifecycle callbacks with the snapshot and final size', () => { | ||
|
|
@@ -438,6 +514,24 @@ describe('Resizable', () => { | |
| expect(getHandle()).toHaveAttribute('aria-controls', getRoot().id); | ||
| }); | ||
|
|
||
| it('does not advertise arrow keys when keyboard resizing is disabled', () => { | ||
| render( | ||
| <Resizable | ||
| data-testid="resizable" | ||
| defaultSize={{ width: 300, height: 200 }} | ||
| > | ||
| <Resizable.Handle | ||
| data-testid="handle" | ||
| direction={[1, 1]} | ||
| disableKeyboardResize | ||
| /> | ||
| </Resizable> | ||
| ); | ||
|
|
||
| expect(getHandle()).toHaveAttribute('role', 'button'); | ||
| expect(getHandle()).not.toHaveAttribute('aria-keyshortcuts'); | ||
| }); | ||
|
|
||
| it('localizes default accessible labels', () => { | ||
| render( | ||
| <Provider locale="ru-RU"> | ||
|
|
||
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.
docs — the
### Intrinsic sizetext just above is now stale.Line 53 still reads “The measured size is fixed in pixels only after the first resize interaction”, which describes the pre-PR behavior. This PR changes exactly that, and its own test edit proves it: the intrinsic-size assertion went from
{ width: '261px', height: '140px' }to width plusstyle.height === ''.A reader following
### Intrinsic sizestill expects both axes to freeze after one drag and gets the opposite, with the contradicting rule buried in the next section. Suggest “the resized axis is fixed in pixels after the first resize interaction”.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.
Reworded: the first resize interaction fixes the resized axes in pixels, the others keep their CSS size.