-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Export menu with Floating UI
- Loading branch information
Showing
7 changed files
with
234 additions
and
98 deletions.
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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import type { ElementProps, FloatingContext } from '@floating-ui/react'; | ||
import { useClickOutside, useKeyboardEvent } from '@react-hookz/web'; | ||
import type { FocusEvent } from 'react'; | ||
import { useCallback, useMemo } from 'react'; | ||
|
||
/* Custom dismiss interaction hook for Floating UI widgets. | ||
* Reduces bundle size (~16 kB) by replacing: | ||
* - `useDismiss` (Escape key, click outside) | ||
* - `FloatingFocusManager` (focus out) */ | ||
export function useFloatingDismiss(context: FloatingContext): ElementProps { | ||
const { refs, onOpenChange } = context; | ||
const { domReference, floating } = refs; | ||
|
||
const handleBlur = useCallback( | ||
(evt: FocusEvent) => { | ||
const { relatedTarget } = evt; | ||
if ( | ||
relatedTarget && | ||
!domReference.current?.contains(relatedTarget) && | ||
!floating.current?.contains(relatedTarget) | ||
) { | ||
onOpenChange(false); | ||
} | ||
}, | ||
[domReference, floating, onOpenChange], | ||
); | ||
|
||
const referenceProps = useMemo(() => ({ onBlur: handleBlur }), [handleBlur]); | ||
const floatingProps = useMemo(() => ({ onBlur: handleBlur }), [handleBlur]); | ||
|
||
useKeyboardEvent('Escape', () => onOpenChange(false)); | ||
useClickOutside(refs.floating, (evt) => { | ||
const triggerRef = refs.domReference.current; | ||
if (evt.target instanceof Element && triggerRef?.contains(evt.target)) { | ||
return; // skip | ||
} | ||
onOpenChange(false); | ||
}); | ||
|
||
return useMemo( | ||
() => ({ reference: referenceProps, floating: floatingProps }), | ||
[referenceProps, floatingProps], | ||
); | ||
} |
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,17 @@ | ||
export async function download( | ||
url: URL | (() => Promise<URL | Blob>), | ||
filename: string, | ||
) { | ||
const urlOrBlob = url instanceof URL ? url : await url(); | ||
|
||
const anchor = document.createElement('a'); | ||
anchor.download = filename; | ||
document.body.append(anchor); | ||
|
||
anchor.href = | ||
urlOrBlob instanceof Blob ? URL.createObjectURL(urlOrBlob) : urlOrBlob.href; | ||
anchor.click(); | ||
|
||
URL.revokeObjectURL(anchor.href); | ||
anchor.remove(); | ||
} |
Oops, something went wrong.