-
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.
Merge pull request #1642 from silx-kit/floating-ui
Refactor Export menu with Floating UI
- Loading branch information
Showing
13 changed files
with
255 additions
and
102 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 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
Binary file modified
BIN
+2 Bytes
(100%)
cypress/snapshots/app.cy.ts/heatmap_2D_inverted_cmap.snap.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,27 @@ | ||
import type { MiddlewareState } from '@floating-ui/react'; | ||
|
||
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(); | ||
} | ||
|
||
export const floatingMinWidth = { | ||
name: 'minWidth', | ||
fn({ x, y, elements, rects }: MiddlewareState) { | ||
elements.floating.style.minWidth = `${rects.reference.width.toString()}px`; | ||
return { x, y }; | ||
}, | ||
}; |
Oops, something went wrong.