-
-
Notifications
You must be signed in to change notification settings - Fork 31
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
Utility hooks additions #867
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
efb668b
Add useLayoutEffect hook for handling SSR in components
kotAPI 5bb3def
Add Radix UI reference for useLayoutEffect implementation
kotAPI 505175c
Add test for composeEventHandlers with checkForDefaultPrevented
kotAPI 3e625db
Refactor TogglePrimitive to use composeEventHandlers
kotAPI 3f4f08d
Update table styles for better visual consistency
kotAPI e69bd7e
Refactor FullHeightScroll component for better styling
kotAPI e277226
Refactor handleTogglePressed function in TogglePrimitive
kotAPI c9e9b57
Refactor useLayoutEffect test cases for SSR and browser
kotAPI File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 7 additions & 3 deletions
10
docs/components/layout/ScrollContainers/FullHeightScroll.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/core/hooks/composeEventHandlers/composeEventHandlers.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import React from 'react'; | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import composeEventHandlers from './index'; | ||
|
||
const OnlyOriginalHandlerWithPreventDefault = ({ | ||
checkForDefaultPrevented = true | ||
}: { | ||
checkForDefaultPrevented?: boolean; | ||
}) => { | ||
const originalClickHandler = (event: React.MouseEvent<HTMLButtonElement>) => { | ||
event.preventDefault(); | ||
// we prevent default, so we should not see our handler | ||
console.log('RETURNING_ORIGINAL_HANDLER'); | ||
}; | ||
const ourClickHandler = () => { | ||
// This won't be triggered because we prevent default in the original handler | ||
console.log('RETURNING_OUR_HANDLER'); | ||
|
||
console.log('RETURN_OUR_HANDLER_WITH_CHECK_FOR_DEFAULT_PREVENTED'); | ||
}; | ||
const composedHandleClick = composeEventHandlers( | ||
originalClickHandler, | ||
ourClickHandler, | ||
{ checkForDefaultPrevented } | ||
); | ||
|
||
return <button onClick={composedHandleClick}>Click Me</button>; | ||
}; | ||
|
||
describe('composeEventHandlers', () => { | ||
let consoleLogSpy: jest.SpyInstance; | ||
|
||
beforeEach(() => { | ||
consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); | ||
}); | ||
|
||
afterEach(() => { | ||
consoleLogSpy.mockRestore(); | ||
}); | ||
|
||
test('should compose event handlers', () => { | ||
render(<OnlyOriginalHandlerWithPreventDefault />); | ||
const button = screen.getByText('Click Me'); | ||
fireEvent.click(button); | ||
expect(consoleLogSpy).toHaveBeenCalledWith('RETURNING_ORIGINAL_HANDLER'); | ||
expect(consoleLogSpy).not.toHaveBeenCalledWith('RETURNING_OUR_HANDLER'); | ||
}); | ||
|
||
test('should compose event handlers with checkForDefaultPrevented false', () => { | ||
// if checkForDefaultPrevented is false, we should see our handler | ||
render(<OnlyOriginalHandlerWithPreventDefault checkForDefaultPrevented={false} />); | ||
const button = screen.getByText('Click Me'); | ||
fireEvent.click(button); | ||
expect(consoleLogSpy).toHaveBeenCalledWith('RETURNING_ORIGINAL_HANDLER'); | ||
|
||
// even if the event is prevented, we should see our handler - the function completely ignores the event prevent default | ||
expect(consoleLogSpy).toHaveBeenCalledWith('RETURN_OUR_HANDLER_WITH_CHECK_FOR_DEFAULT_PREVENTED'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
type EventHandler<E = React.SyntheticEvent> = (event: E) => void; | ||
|
||
// From Radix UI - packages/core/primitive/src/primitive.tsx | ||
|
||
const composeEventHandlers = <E extends React.SyntheticEvent>( | ||
originalEventHandler?: EventHandler<E>, | ||
ourEventHandler?: EventHandler<E>, | ||
{ checkForDefaultPrevented = true } = {} | ||
) => { | ||
// Returns a function that handles the event | ||
return function handleEvent(event: E) { | ||
// If the original event handler is defined, call it | ||
if (typeof originalEventHandler === 'function') { | ||
originalEventHandler(event); | ||
} | ||
|
||
// If the default prevented flag is false or the event is not prevented, call our event handler | ||
if (checkForDefaultPrevented === false || !event.defaultPrevented) { | ||
return ourEventHandler?.(event); | ||
} | ||
}; | ||
}; | ||
|
||
export default composeEventHandlers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// A simple wrapper for useLayoutEffect that does not throw an error on server components | ||
|
||
import { useLayoutEffect as ReactUseLayoutEffect } from 'react'; | ||
|
||
// When we use useLayoutEffect in a server component, it will throw an error. | ||
// One of the hooks that do not work on server components | ||
// wrapping it this way and using it will make sure errors are not thrown and fail silently | ||
// If it's server, we just return a noop function (no operation) | ||
|
||
// Radix UI does it this way - https://github.com/radix-ui/primitives/blob/main/packages/react/use-layout-effect/src/use-layout-effect.tsx | ||
// | ||
const useLayoutEffect = globalThis.document ? ReactUseLayoutEffect : () => {}; | ||
|
||
export default useLayoutEffect; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React from 'react'; | ||
import useLayoutEffect from './index'; | ||
import { render } from '@testing-library/react'; | ||
|
||
const TestComponent = () => { | ||
// invoke useLayoutEffect and check if the component still mounts as expected | ||
useLayoutEffect(() => { | ||
// mounts | ||
}, []); | ||
return <div>Hello</div>; | ||
}; | ||
|
||
describe('useLayoutEffect', () => { | ||
it('should mount without errors in SSR environment', () => { | ||
render(<TestComponent />); | ||
}); | ||
|
||
it('should execute effect in browser environment', () => { | ||
const mockFn = jest.fn(); | ||
const TestComponent = () => { | ||
useLayoutEffect(() => { | ||
mockFn(); | ||
}, []); | ||
return <div>Hello</div>; | ||
}; | ||
|
||
// Mock document existence | ||
const originalDocument = global.document; | ||
global.document = {} as typeof document; | ||
|
||
render(<TestComponent />); | ||
expect(mockFn).toHaveBeenCalledTimes(1); | ||
|
||
// Cleanup | ||
global.document = originalDocument; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🛠️ Refactor suggestion
Improve browser environment test robustness.
The browser environment test could be more robust with these improvements:
📝 Committable suggestion