-
Notifications
You must be signed in to change notification settings - Fork 793
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/floating UI toolbar dropdown (#2027)
* toolbar opens where the button is * udpated positioning of toolbar dropdown with flyout-ui * fix: test * Create good-carpets-drive.md Co-authored-by: Ziad Beyens <[email protected]>
- Loading branch information
1 parent
97babd6
commit 476466e
Showing
5 changed files
with
98 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@udecode/plate-ui-font": patch | ||
"@udecode/plate-ui-toolbar": patch | ||
--- | ||
|
||
Feat/floating UI toolbar dropdown |
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
65 changes: 65 additions & 0 deletions
65
packages/ui/toolbar/src/ToolbarDropdown/useDropdownControls.ts
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,65 @@ | ||
import { useEffect } from 'react'; | ||
import { ExtendedRefs, flip, useFloating } from '@udecode/plate-floating'; | ||
|
||
type useDropdownControlsProps = { | ||
open: boolean; | ||
onClose?: (ev: MouseEvent) => void; | ||
}; | ||
|
||
const closeAllExceptSelectedOneListener = ({ | ||
open, | ||
onClose, | ||
refs, | ||
}: useDropdownControlsProps & { refs: ExtendedRefs<HTMLElement> }) => ( | ||
ev: MouseEvent | ||
) => { | ||
if (open) { | ||
const target = ev.target as HTMLElement; | ||
if (refs.reference.current?.contains(target)) { | ||
return; | ||
} | ||
if (refs.floating.current?.contains(target)) { | ||
return; | ||
} | ||
|
||
onClose?.(ev); | ||
} | ||
}; | ||
|
||
export const useDropdownControls = ({ | ||
open, | ||
onClose, | ||
}: useDropdownControlsProps) => { | ||
const { | ||
x, | ||
y, | ||
reference, | ||
floating, | ||
strategy, | ||
refs, | ||
} = useFloating<HTMLElement>({ | ||
open, | ||
strategy: 'fixed', | ||
placement: 'bottom-start', | ||
middleware: [flip()], | ||
}); | ||
|
||
useEffect(() => { | ||
const listener = closeAllExceptSelectedOneListener({ open, onClose, refs }); | ||
document.body.addEventListener('mousedown', listener); | ||
return () => { | ||
document.body.removeEventListener('mousedown', listener); | ||
}; | ||
}, [onClose, open, refs]); | ||
|
||
return { | ||
styles: { | ||
position: strategy, | ||
top: y ?? 0, | ||
left: x ?? 0, | ||
width: 'max-content', | ||
}, | ||
reference, | ||
floating, | ||
}; | ||
}; |
476466e
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.
Successfully deployed to the following URLs:
plate – ./
plate-udecode.vercel.app
plate-git-main-udecode.vercel.app
plate.udecode.io
www.plate.udecode.io
476466e
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.
Successfully deployed to the following URLs:
plate-examples – ./
plate-examples-git-main-udecode.vercel.app
plate-examples.vercel.app
plate-examples-udecode.vercel.app