Skip to content

开源白板工具(SaaS),一体化白板,包含思维导图、流程图、自由画等。All in one open-source whiteboard tool with mind, flowchart, freehand and etc.

refactor(popup): use popover to replace manual using floating-ui

pubuzhixing8d594cc73f131
diff --git a/packages/drawnix/src/components/toolbar/popup-toolbar/fill-button.tsx b/packages/drawnix/src/components/toolbar/popup-toolbar/fill-button.tsx
index d001ff0..912535a 100644
--- a/packages/drawnix/src/components/toolbar/popup-toolbar/fill-button.tsx
+++ b/packages/drawnix/src/components/toolbar/popup-toolbar/fill-button.tsx
@@ -1,23 +1,15 @@
import React, { useState } from 'react';
import { ToolButton } from '../../tool-button';
import classNames from 'classnames';
-import {
- autoUpdate,
- flip,
- offset,
- useClick,
- useDismiss,
- useFloating,
- useInteractions,
- useRole,
-} from '@floating-ui/react';
-import { ATTACHED_ELEMENT_CLASS_NAME } from '@plait/core';
+import { ATTACHED_ELEMENT_CLASS_NAME, PlaitBoard } from '@plait/core';
import { Island } from '../../island';
import { ColorPicker } from '../../color-picker';
import { isTransparent, removeHexAlpha } from '../../../utils/color';
import { BackgroundColorIcon } from '../../icons';
+import { Popover, PopoverContent, PopoverTrigger } from '../../popover/popover';
export type PopupFillButtonProps = {
+ board: PlaitBoard;
currentColor: string | undefined;
title: string;
children?: React.ReactNode;
@@ -25,49 +17,44 @@ export type PopupFillButtonProps = {
};
export const PopupFillButton: React.FC<PopupFillButtonProps> = ({
+ board,
currentColor,
title,
children,
onColorSelect,
}) => {
- const [isOpen, setIsOpen] = useState(false);
- const { refs, floatingStyles, context } = useFloating({
- placement: 'left',
- open: isOpen,
- onOpenChange: setIsOpen,
- whileElementsMounted: autoUpdate,
- middleware: [offset(8), flip()],
- });
-
- const click = useClick(context);
- const dismiss = useDismiss(context);
- const role = useRole(context);
-
- const { getReferenceProps } = useInteractions([click, dismiss, role]);
-
+ const [isFillPropertyOpen, setIsFillPropertyOpen] = useState(false);
const hexColor = currentColor && removeHexAlpha(currentColor);
+ const container = PlaitBoard.getBoardContainer(board);
let icon =
!hexColor || isTransparent(hexColor) ? BackgroundColorIcon : undefined;
return (
- <>
- <ToolButton
- className={classNames(`property-button`)}
- visible={true}
- icon={icon}
- type="button"
- title={title}
- aria-label={title}
- ref={refs.setReference}
- {...getReferenceProps()}
- >
- {!icon && children}
- </ToolButton>
- {isOpen && (
+ <Popover
+ sideOffset={12}
+ open={isFillPropertyOpen}
+ onOpenChange={(open) => {
+ setIsFillPropertyOpen(open);
+ }}
+ >
+ <PopoverTrigger asChild>
+ <ToolButton
+ className={classNames(`property-button`)}
+ visible={true}
+ icon={icon}
+ type="button"
+ title={title}
+ aria-label={title}
+ onPointerUp={() => {
+ setIsFillPropertyOpen(!isFillPropertyOpen);
+ }}
+ >
+ {!icon && children}
+ </ToolButton>
+ </PopoverTrigger>
+ <PopoverContent container={container}>
<Island
- ref={refs.setFloating}
- style={floatingStyles}
padding={4}
className={classNames(`${ATTACHED_ELEMENT_CLASS_NAME}`)}
>
@@ -78,7 +65,7 @@ export const PopupFillButton: React.FC<PopupFillButtonProps> = ({
currentColor={currentColor}
></ColorPicker>
</Island>
- )}
- </>
+ </PopoverContent>
+ </Popover>
);
};
diff --git a/packages/drawnix/src/components/toolbar/popup-toolbar/font-color-button.tsx b/packages/drawnix/src/components/toolbar/popup-toolbar/font-color-button.tsx
index ab0d05d..af49eda 100644
--- a/packages/drawnix/src/components/toolbar/popup-toolbar/font-color-button.tsx
+++ b/packages/drawnix/src/components/toolbar/popup-toolbar/font-color-button.tsx
@@ -2,20 +2,12 @@ import React, { ReactNode, useState } from 'react';
import { ColorPicker } from '../../color-picker';
import { ToolButton } from '../../tool-button';
import classNames from 'classnames';
-import {
- autoUpdate,
- flip,
- offset,
- useClick,
- useDismiss,
- useFloating,
- useInteractions,
- useRole,
-} from '@floating-ui/react';
import { Island } from '../../island';
-import { ATTACHED_ELEMENT_CLASS_NAME } from '@plait/core';
+import { ATTACHED_ELEMENT_CLASS_NAME, PlaitBoard } from '@plait/core';
+import { Popover, PopoverContent, PopoverTrigger } from '../../popover/popover';
export type PopupFontColorButtonProps = {
+ board: PlaitBoard;
currentColor: string | undefined;
fontColorIcon: ReactNode;
title: string;
@@ -23,53 +15,52 @@ export type PopupFontColorButtonProps = {
};
export const PopupFontColorButton: React.FC<PopupFontColorButtonProps> = ({
+ board,
currentColor,
fontColorIcon,
title,
onSelect,
}) => {
- const [isOpen, setIsOpen] = useState(false);
- const { refs, floatingStyles, context } = useFloating({
- placement: 'left',
- open: isOpen,
- onOpenChange: setIsOpen,
- whileElementsMounted: autoUpdate,
- middleware: [offset(8), flip()],
- });
-
- const click = useClick(context);
- const dismiss = useDismiss(context);
- const role = useRole(context);
-
- const { getReferenceProps } = useInteractions([click, dismiss, role]);
+ const [isFontColorPropertyOpen, setIsFontColorPropertyOpen] = useState(false);
+ const container = PlaitBoard.getBoardContainer(board);
return (
<>
- <ToolButton
- className={classNames(`property-button`)}
- visible={true}
- icon={fontColorIcon}
- type="button"
- title={title}
- aria-label={title}
- ref={refs.setReference}
- {...getReferenceProps()}
- ></ToolButton>
- {isOpen && (
- <Island
- ref={refs.setFloating}
- style={floatingStyles}
- padding={4}
- className={classNames(`${ATTACHED_ELEMENT_CLASS_NAME}`)}
- >
- <ColorPicker
- onSelect={(selectedColor) => {
- onSelect(selectedColor);
+ <Popover
+ sideOffset={12}
+ open={isFontColorPropertyOpen}
+ onOpenChange={(open) => {
+ setIsFontColorPropertyOpen(open);
+ }}
+ >
+ <PopoverTrigger asChild>
+ <ToolButton
+ className={classNames(`property-button`)}
+ selected={isFontColorPropertyOpen}
+ visible={true}
+ icon={fontColorIcon}
+ type="button"
+ title={title}
+ aria-label={title}
+ onPointerUp={() => {
+ setIsFontColorPropertyOpen(!isFontColorPropertyOpen);
}}
- currentColor={currentColor}
- ></ColorPicker>
- </Island>
- )}
+ ></ToolButton>
+ </PopoverTrigger>
+ <PopoverContent container={container}>
+ <Island
+ padding={4}
+ className={classNames(`${ATTACHED_ELEMENT_CLASS_NAME}`)}
+ >
+ <ColorPicker
+ onSelect={(selectedColor) => {
+ onSelect(selectedColor);
+ }}
+ currentColor={currentColor}
+ ></ColorPicker>
+ </Island>
+ </PopoverContent>
+ </Popover>
</>
);
};
diff --git a/packages/drawnix/src/components/toolbar/popup-toolbar/popup-toolbar.tsx b/packages/drawnix/src/components/toolbar/popup-toolbar/popup-toolbar.tsx
index 69c553d..f6d49be 100644
--- a/packages/drawnix/src/components/toolbar/popup-toolbar/popup-toolbar.tsx
+++ b/packages/drawnix/src/components/toolbar/popup-toolbar/popup-toolbar.tsx
@@ -148,6 +148,7 @@ export const PopupToolbar: React.FC<PopupToolbarProps> = ({}) => {
<Stack.Row gap={1}>
{state.hasFontColor && (
<PopupFontColorButton
+ board={board}
key={0}
currentColor={state.marks?.color}
title={`Font Color`}
@@ -165,6 +166,7 @@ export const PopupToolbar: React.FC<PopupToolbarProps> = ({}) => {
></PopupFontColorButton>
)}
<PopupStrokeButton
+ board={board}
key={1}
currentColor={state.strokeColor}
title={`Stroke`}
@@ -197,6 +199,7 @@ export const PopupToolbar: React.FC<PopupToolbarProps> = ({}) => {
</PopupStrokeButton>
{state.hasFill && (
<PopupFillButton
+ board={board}
key={2}
currentColor={state.fill}
title={`Fill Color`}
diff --git a/packages/drawnix/src/components/toolbar/popup-toolbar/stroke-button.tsx b/packages/drawnix/src/components/toolbar/popup-toolbar/stroke-button.tsx
index 6f50485..c3a1406 100644
--- a/packages/drawnix/src/components/toolbar/popup-toolbar/stroke-button.tsx
+++ b/packages/drawnix/src/components/toolbar/popup-toolbar/stroke-button.tsx
@@ -1,23 +1,15 @@
import React, { useState } from 'react';
import { ToolButton } from '../../tool-button';
import classNames from 'classnames';
-import {
- autoUpdate,
- flip,
- offset,
- useClick,
- useDismiss,
- useFloating,
- useInteractions,
- useRole,
-} from '@floating-ui/react';
-import { ATTACHED_ELEMENT_CLASS_NAME } from '@plait/core';
+import { ATTACHED_ELEMENT_CLASS_NAME, PlaitBoard } from '@plait/core';
import { Island } from '../../island';
import { ColorPicker } from '../../color-picker';
import { isTransparent, isWhite, removeHexAlpha } from '../../../utils/color';
import { StrokeIcon, StrokeWhiteIcon } from '../../icons';
+import { Popover, PopoverContent, PopoverTrigger } from '../../popover/popover';
export type PopupStrokeButtonProps = {
+ board: PlaitBoard;
currentColor: string | undefined;
title: string;
children?: React.ReactNode;
@@ -25,27 +17,15 @@ export type PopupStrokeButtonProps = {
};
export const PopupStrokeButton: React.FC<PopupStrokeButtonProps> = ({
+ board,
currentColor,
title,
children,
onColorSelect,
}) => {
- const [isOpen, setIsOpen] = useState(false);
- const { refs, floatingStyles, context } = useFloating({
- placement: 'left',
- open: isOpen,
- onOpenChange: setIsOpen,
- whileElementsMounted: autoUpdate,
- middleware: [offset(8), flip()],
- });
-
- const click = useClick(context);
- const dismiss = useDismiss(context);
- const role = useRole(context);
-
- const { getReferenceProps } = useInteractions([click, dismiss, role]);
-
+ const [isStrokePropertyOpen, setIsStrokePropertyOpen] = useState(false);
const hexColor = currentColor && removeHexAlpha(currentColor);
+ const container = PlaitBoard.getBoardContainer(board);
let icon = isTransparent(hexColor)
? StrokeIcon
@@ -55,33 +35,42 @@ export const PopupStrokeButton: React.FC<PopupStrokeButtonProps> = ({
return (
<>
- <ToolButton
- className={classNames(`property-button`)}
- visible={true}
- icon={icon}
- type="button"
- title={title}
- aria-label={title}
- ref={refs.setReference}
- {...getReferenceProps()}
+ <Popover
+ sideOffset={12}
+ open={isStrokePropertyOpen}
+ onOpenChange={(open) => {
+ setIsStrokePropertyOpen(open);
+ }}
>
- {!icon && children}
- </ToolButton>
- {isOpen && (
- <Island
- ref={refs.setFloating}
- style={floatingStyles}
- padding={4}
- className={classNames(`${ATTACHED_ELEMENT_CLASS_NAME}`)}
- >
- <ColorPicker
- onSelect={(selectedColor) => {
- onColorSelect(selectedColor);
+ <PopoverTrigger asChild>
+ <ToolButton
+ className={classNames(`property-button`)}
+ visible={true}
+ icon={icon}
+ type="button"
+ title={title}
+ aria-label={title}
+ onPointerUp={() => {
+ setIsStrokePropertyOpen(!isStrokePropertyOpen);
}}
- currentColor={currentColor}
- ></ColorPicker>
- </Island>
- )}
+ >
+ {!icon && children}
+ </ToolButton>
+ </PopoverTrigger>
+ <PopoverContent container={container}>
+ <Island
+ padding={4}
+ className={classNames(`${ATTACHED_ELEMENT_CLASS_NAME}`)}
+ >
+ <ColorPicker
+ onSelect={(selectedColor) => {
+ onColorSelect(selectedColor);
+ }}
+ currentColor={currentColor}
+ ></ColorPicker>
+ </Island>
+ </PopoverContent>
+ </Popover>
</>
);
};