From a8b160aa450fe23466aa978a2cfd7900c72e8b46 Mon Sep 17 00:00:00 2001 From: folinhilo Date: Sun, 22 Nov 2026 04:09:28 +0800 Subject: [PATCH] mm --- packages/ui-react/gallery/app.tsx | 2 + packages/ui-react/gallery/menu-gallery.tsx | 54 +++++++ .../ui-react/src/components/button/button.tsx | 9 +- .../src/components/checkbox/checkbox.tsx | 77 ++++++--- .../src/components/menu/menu-check-item.tsx | 152 ++++++++++++------ .../src/components/menu/menu-dropdown.tsx | 78 +++++---- .../src/components/menu/menu-item.tsx | 1 + .../components/menu/menu-radio-item-group.tsx | 32 ++-- .../components/popover/popover-dropdown.tsx | 1 - packages/ui-react/src/index.css | 1 - packages/ui-react/src/styles/css/gap.css | 19 ++- .../ui-react/src/styles/css/padding-long.css | 20 --- .../ui-react/src/styles/css/theme-base.css | 26 ++- .../src/styles/recipes/item-size-recipe.ts | 8 - 14 files changed, 304 insertions(+), 176 deletions(-) create mode 100644 packages/ui-react/gallery/menu-gallery.tsx delete mode 100644 packages/ui-react/src/styles/css/padding-long.css diff --git a/packages/ui-react/gallery/app.tsx b/packages/ui-react/gallery/app.tsx index 997f177..f0a4311 100644 --- a/packages/ui-react/gallery/app.tsx +++ b/packages/ui-react/gallery/app.tsx @@ -1,6 +1,7 @@ import { ButtonGallery } from "./button-gallery"; import { Theme } from "../src/components/theme/theme"; import { PopoverGallery } from "./popover-gallery"; +import { MenuGallery } from "./menu-gallery"; export function App() { return ( @@ -8,6 +9,7 @@ export function App() { + ); diff --git a/packages/ui-react/gallery/menu-gallery.tsx b/packages/ui-react/gallery/menu-gallery.tsx new file mode 100644 index 0000000..63ef22d --- /dev/null +++ b/packages/ui-react/gallery/menu-gallery.tsx @@ -0,0 +1,54 @@ +import { + Button, + Menu, + MenuCheckItem, + MenuDropdown, + MenuItem, + MenuRadioItem, + MenuRadioItemGroup, + MenuTrigger, +} from "../src"; +import { IW } from "./iw"; +import { OW } from "./ow"; + +export const MenuGallery = () => { + return ( + + + + + + + + Open File... + Open Folder... + + + Preferences + + + Profile + Settings + Auto Lint + + + + + Ruler + + + + Hori + Verti + Hoth + + + + + + + + ); +}; diff --git a/packages/ui-react/src/components/button/button.tsx b/packages/ui-react/src/components/button/button.tsx index 14c943d..8862fa5 100644 --- a/packages/ui-react/src/components/button/button.tsx +++ b/packages/ui-react/src/components/button/button.tsx @@ -8,6 +8,7 @@ import { brandRecipe } from "../../styles/recipes/brand-recipe"; import type { CommonProps } from "../../utils/CommonProps"; import { mergeClass } from "../../styles/utils/merge-class"; +// 缺个 loading 动画,暂时没想好怎么做,后面发现样本再抄 export type ButtonProps = CommonProps & { size?: "xs" | "sm" | "md" | "lg"; variant?: "filled" | "outline" | "subtle" | "ghost"; @@ -54,11 +55,6 @@ export const Button = (props: ButtonProps) => { ...rest } = props; - const [innerIsLoading, setInnerIsIsLoading] = useState<{ - state: boolean; - class: string; - }>({ state: false, class: "" }); - const brandFinal = brand != undefined ? brand : variant == "filled" ? "info" : "default"; @@ -66,14 +62,13 @@ export const Button = (props: ButtonProps) => { const rootClass = mergeClass( displayRecipe(), - itemSizeRecipe({ size, shape, isIconOnly, isLongPadding: true }), + itemSizeRecipe({ size, shape, isIconOnly}), variantRecipe({ variant, isDisabled }), brandRecipe({ brand: brandFinal }), className, ); let rootStyle: CSSProperties = { - transitionDuration: "all 5s ease-in-out", width: width, ...style, }; diff --git a/packages/ui-react/src/components/checkbox/checkbox.tsx b/packages/ui-react/src/components/checkbox/checkbox.tsx index 1f62d83..21a3f52 100644 --- a/packages/ui-react/src/components/checkbox/checkbox.tsx +++ b/packages/ui-react/src/components/checkbox/checkbox.tsx @@ -1,36 +1,73 @@ -import { useState, type ComponentPropsWithRef } from "react"; +import { useRef, useState, type ChangeEvent, type MouseEvent } from "react"; import { CheckIndicatorSvg } from "../../assets/svg/CheckIndicatorSvg"; import type { CommonProps } from "../../utils/CommonProps"; import { mergeClass } from "../../styles/utils/merge-class"; import { inlineSizeRecipe } from "../../styles/recipes/inline-size-recipe"; +import { useMergeRefs } from "@floating-ui/react"; export type CheckboxProps = CommonProps & { - size?: "sm" | "md", - defaultCheck?: boolean, - onChange?: (preCheck?: boolean, nextCheck?: boolean) => void, - controllerCheck?: boolean + size?: "sm" | "md"; + checked?: boolean; + defaultChecked?: boolean; + onChange?: (nextChecked: boolean) => void; }; export const Checkbox = (props: CheckboxProps) => { - const { defaultCheck = false, ref, onChange, size = "sm", className = "" } = props + const { + size = "sm", + checked: controlledChecked, + defaultChecked = false, + onChange, + ref, + className = "", + } = props; - const [isCheck, setIsCheck] = useState(defaultCheck) + const [checked, setChecked] = useState( + controlledChecked ?? defaultChecked, + ); - const rootClass = mergeClass(inlineSizeRecipe({ size, shape: "rounded", isIconOnly: true }), className) + const inputRef = useRef(null); - const inputClass = mergeClass("sr-only") + const rootClass = mergeClass( + inlineSizeRecipe({ size, shape: "rounded", isIconOnly: true }), + className, + ); - const indicatorClass = mergeClass(inlineSizeRecipe({ size, shape: "rounded" })) + const inputClass = mergeClass("sr-only"); - function clickHandler() { - const preValue = isCheck - setIsCheck(!isCheck) - onChange?.(preValue, isCheck) - } + const indicatorClass = mergeClass( + inlineSizeRecipe({ size, shape: "rounded" }), + ); - return
- - {isCheck && } + // 将 input 的 onClick 范围扩大至整个容器元素 + function clickHandler(e: MouseEvent) { + // 防止 input 和 div 重叠,响应两次 onClick + e.stopPropagation(); + // 记住前面没有 on + inputRef?.current?.click(); + } + + // 绑定 state 第2/3步,本元素被键盘点击后,将新值写回 state + function changeHandler(e: ChangeEvent) { + // 触发重新渲染,受控模式下,即使 set 也改变不了状态 + setChecked(e.target.checked); + // 执行受控回调函数,向上层传递新值,或许上层组件会拒绝变更从 prop 传入的 checked + onChange?.(e.target.checked); + } + + return ( +
+ + {checked && }
-} -Checkbox.displayName = "Checkbox" \ No newline at end of file + ); +}; +Checkbox.displayName = "Checkbox"; diff --git a/packages/ui-react/src/components/menu/menu-check-item.tsx b/packages/ui-react/src/components/menu/menu-check-item.tsx index 13dae17..65680bf 100644 --- a/packages/ui-react/src/components/menu/menu-check-item.tsx +++ b/packages/ui-react/src/components/menu/menu-check-item.tsx @@ -1,8 +1,12 @@ - import { useMenuFloatingContext } from "./use-menu-floating-context"; import { useListItem, useMergeRefs } from "@floating-ui/react"; import { useMenuContext } from "./use-menu-context"; -import React, { useState, type CSSProperties } from "react"; +import React, { + useRef, + useState, + type CSSProperties, + type MouseEvent, +} from "react"; import { Icon } from "../icon/icon"; import { Checkbox } from "../checkbox/checkbox"; import { displayRecipe } from "../../styles/recipes/display-recipe"; @@ -13,74 +17,116 @@ import type { CommonProps } from "../../utils/CommonProps"; import { containerRecipe } from "../../styles/recipes/container-recipe"; export type MenuCheckItemProps = CommonProps & { - iconSvg?: React.ReactNode; - hotkey?: string; - check?: boolean - onChange?: (preCheck?: boolean, nextCheck?: boolean) => void + iconSvg?: React.ReactNode; + hotkey?: string; + checked?: boolean; + defaultChecked?: boolean; + onChange?: (nextChecked: boolean) => void; }; export const MenuCheckItem = (props: MenuCheckItemProps) => { - const { ref, children, iconSvg =
, style, hotkey, isDisabled = false, onChange, ...rest } = props; + const { + checked: controlledChecked, + defaultChecked = false, + // ============================== + onChange, + ref, + children, + iconSvg =
, + isDisabled = false, + hotkey, + style, + ...rest + } = props; - const [isCheck, setIsCheck] = useState(false) + const [checked, setChecked] = useState( + controlledChecked ?? defaultChecked, + ); + const inputRef = useRef(null); - // =============== Menu Context ==================================================================== + // =============== Menu Context ==================================================================== - const menuContext = useMenuContext(); - const { size = "sm" } = menuContext! + const menuContext = useMenuContext(); + const { size = "sm" } = menuContext!; - // =============== Menu Floating Context ==================================================================== + // =============== Menu Floating Context ==================================================================== - const menuFloatingContext = useMenuFloatingContext(); - const { getItemProps, activeIndex, isOpen, } = menuFloatingContext! + const menuFloatingContext = useMenuFloatingContext(); + const { getItemProps, activeIndex, isOpen } = menuFloatingContext!; - const item = useListItem(); - const isActive = item.index === activeIndex; + const item = useListItem(); + const isActive = item.index === activeIndex; - // ================== Style ================================================================= + // ================== Style ================================================================= - const rootClass = mergeClass(displayRecipe({ justifyContent: "between", }), containerRecipe({ hasFocusOutline: "false" }), - itemSizeRecipe({ size, shape: "rounded" }), - variantRecipe({ variant: "ghost", isDisabled })) + const rootClass = mergeClass( + displayRecipe({ justifyContent: "between" }), + containerRecipe({ hasFocusOutline: "false" }), + itemSizeRecipe({ size, shape: "rounded" }), + variantRecipe({ variant: "ghost", isDisabled }), + ); - const rootStyle: CSSProperties = { - width: "100%", - ...style - } + const rootStyle: CSSProperties = { + width: "100%", + ...style, + }; - const startSectionClass = displayRecipe() + const startSectionClass = displayRecipe(); - const endSectionClass = displayRecipe() + const endSectionClass = displayRecipe(); - function handleOnClick() { - const preCheck = isCheck - setIsCheck(!isCheck) - onChange?.(preCheck, isCheck) - } + // 将 Checkbox 的 onClick 范围扩大至整个容器元素 + function clickHandler(e: MouseEvent) { + // 防止 input 和 div 重叠,响应两次 onClick + e.stopPropagation(); + // 前面没有 on + inputRef?.current?.click(); + } - return +
+ {iconSvg} + {children} +
+
+ {hotkey &&
{hotkey}
} + {/* 占位 */} + +
+
+
+ + ); }; diff --git a/packages/ui-react/src/components/menu/menu-dropdown.tsx b/packages/ui-react/src/components/menu/menu-dropdown.tsx index 6d76558..b38c639 100644 --- a/packages/ui-react/src/components/menu/menu-dropdown.tsx +++ b/packages/ui-react/src/components/menu/menu-dropdown.tsx @@ -7,49 +7,71 @@ import { import { useMenuFloatingContext } from "./use-menu-floating-context"; import { useMenuContext } from "./use-menu-context"; import { mergeClass } from "../../styles/utils/merge-class"; -import { containerRecipe } from "../../styles/recipes/container-recipe" +import { containerRecipe } from "../../styles/recipes/container-recipe"; import { useThemeContext } from "../theme/use-theme-context"; +import type { CommonProps } from "../../utils/CommonProps"; +import { brandRecipe } from "../../styles/recipes/brand-recipe"; - -export type MenuDropdownProps = ComponentPropsWithoutRef<"div"> & { +export type MenuDropdownProps = CommonProps & { modal?: boolean; }; export const MenuDropdown = (props: MenuDropdownProps) => { - const { modal = true, className = "", children, ...rest } = props; + const { modal = true, className = "", children, style, ...rest } = props; - const { size } = useMenuContext()! - const { refs, elementsRef, context, floatingStyles, getFloatingProps, isNested, isOpen } = useMenuFloatingContext()!; + const { size } = useMenuContext()!; + const { + refs, + elementsRef, + context, + floatingStyles, + getFloatingProps, + isNested, + isOpen, + } = useMenuFloatingContext()!; // ============================================================================================================== - const { themeBaseClass, themeClass } = useThemeContext()! + const { themeBaseClass, themeClass } = useThemeContext()!; // =============================================================================================================== - const rootClass = mergeClass(themeBaseClass, themeClass, containerRecipe(), className); - - const padding = size == "sm" ? "4px" : (size == "md" ? "8px" : undefined); - const borderRadius = size == "sm" ? "4px" : (size == "md" ? "8px" : undefined); + const rootClass = mergeClass( + containerRecipe({ + boxShadow: true, + rounded: size == "sm" ? 4 : 8, + padding: size == "sm" ? 4 : 8, + background: "base", + hasFocusOutline: "false", + }), + brandRecipe(), + themeBaseClass, + themeClass, + className, + ); const rootStyle: CSSProperties = { ...floatingStyles, - borderRadius: borderRadius, - padding: padding, - // invisable the outline - outline: 0, - background: "var(--base-bg)" + ...style, + }; - } - - return - {isOpen && - -
- {children} -
-
-
- } -
+ return ( + + {isOpen && ( + + +
+ {children} +
+
+
+ )} +
+ ); }; diff --git a/packages/ui-react/src/components/menu/menu-item.tsx b/packages/ui-react/src/components/menu/menu-item.tsx index 576e001..8d65e47 100644 --- a/packages/ui-react/src/components/menu/menu-item.tsx +++ b/packages/ui-react/src/components/menu/menu-item.tsx @@ -97,6 +97,7 @@ export const MenuItem = (props: MenuItemProps) => { )} {isTrigger && !isOpen && {indicatorSvg}} {!isTrigger && ( + // 占位
diff --git a/packages/ui-react/src/components/menu/menu-radio-item-group.tsx b/packages/ui-react/src/components/menu/menu-radio-item-group.tsx index acf4f63..00f3686 100644 --- a/packages/ui-react/src/components/menu/menu-radio-item-group.tsx +++ b/packages/ui-react/src/components/menu/menu-radio-item-group.tsx @@ -1,24 +1,34 @@ -import { useState } from "react"; +import { useEffect, useRef, useState } from "react"; import type { CommonProps } from "../../utils/CommonProps"; import { MenuRadioItemGroupContext } from "./use-menu-radio-item-group-context"; import { useMenuContext } from "./use-menu-context"; export type MenuRadioItemGroupProps = CommonProps & { - size?: "sm" | "md"; - defaultSelectName?: string + size?: "sm" | "md"; + defaultSelectName?: string; + onChange?: (nextSelect?: string) => void; }; export const MenuRadioItemGroup = (props: MenuRadioItemGroupProps) => { - const { size = "sm", defaultSelectName = "", ...rest } = props + const { + size = "sm", + defaultSelectName = "", + children, + onChange, + ...rest + } = props; - const [selectName, setSelectName] = useState(defaultSelectName); + const [selectName, setSelectName] = useState(defaultSelectName); - const { size: menuSize } = useMenuContext()! + const { size: menuSize } = useMenuContext()!; - const currentSize = menuSize ?? size + const currentSize = menuSize ?? size; - return -
+ return ( + +
{children}
- -} \ No newline at end of file + ); +}; diff --git a/packages/ui-react/src/components/popover/popover-dropdown.tsx b/packages/ui-react/src/components/popover/popover-dropdown.tsx index 21d2a4a..f294115 100644 --- a/packages/ui-react/src/components/popover/popover-dropdown.tsx +++ b/packages/ui-react/src/components/popover/popover-dropdown.tsx @@ -8,7 +8,6 @@ import { } from "@floating-ui/react"; import { useThemeContext } from "../theme/use-theme-context"; import { mergeClass } from "../../styles/utils/merge-class"; -import { variantRecipe } from "../../styles/recipes/variant-recipe"; import { brandRecipe } from "../../styles/recipes/brand-recipe"; import { containerRecipe } from "../../styles/recipes/container-recipe"; diff --git a/packages/ui-react/src/index.css b/packages/ui-react/src/index.css index fcd2c34..a288e04 100644 --- a/packages/ui-react/src/index.css +++ b/packages/ui-react/src/index.css @@ -16,7 +16,6 @@ @import './styles/css/position.css'; @import './styles/css/pointer-events.css'; @import './styles/css/padding.css'; -@import './styles/css/padding-long.css'; @import './styles/css/overscroll-behavior.css'; @import './styles/css/overflow.css'; @import './styles/css/outline.css'; diff --git a/packages/ui-react/src/styles/css/gap.css b/packages/ui-react/src/styles/css/gap.css index 6e69176..7c01b31 100644 --- a/packages/ui-react/src/styles/css/gap.css +++ b/packages/ui-react/src/styles/css/gap.css @@ -1,34 +1,33 @@ @layer utility { - .gap-none { - gap: none; + gap: var(--gap-none); } .gap--xs { - gap: 2px; + gap: var(--gap-xs); } .gap--sm { - gap: 4px; + gap: var(--gap-sm); } .gap--md { - gap: 6px; + gap: var(--gap-md); } .gap--lg { - gap: 8px; + gap: var(--gap-lg); } .gap--xl { - gap: 10px; + gap: var(--gap-lg); } .gap--2xl { - gap: 12px; + gap: var(--gap-2xl); } .gap--3xl { - gap: 14px; + gap: var(--gap-3xl); } -} \ No newline at end of file +} diff --git a/packages/ui-react/src/styles/css/padding-long.css b/packages/ui-react/src/styles/css/padding-long.css deleted file mode 100644 index e43939d..0000000 --- a/packages/ui-react/src/styles/css/padding-long.css +++ /dev/null @@ -1,20 +0,0 @@ -@layer utility { - .px--long-xs { - padding-inline: var(--padding-long-xs); - } - .px--long-sm { - padding-inline: var(--padding-long-sm); - } - .px--long-md { - padding-inline: var(--padding-long-md); - } - .px--long-lg { - padding-inline: var(--padding-long-lg); - } - .px--long-xl { - padding-inline: var(--padding-long-xl); - } - .px--long-2xl { - padding-inline: var(--padding-long-2xl); - } -} diff --git a/packages/ui-react/src/styles/css/theme-base.css b/packages/ui-react/src/styles/css/theme-base.css index aeb75f0..857383c 100644 --- a/packages/ui-react/src/styles/css/theme-base.css +++ b/packages/ui-react/src/styles/css/theme-base.css @@ -358,8 +358,8 @@ --height-item-xs: 20px; --height-item-sm: 24px; --height-item-md: 28px; - --height-item-lg: 34px; - --height-item-xl: 44px; + --height-item-lg: 32px; + --height-item-xl: 42px; --height-item-2xl: 56px; --height-item-3xl: 64px; @@ -373,26 +373,18 @@ --radius-none: 0; --radius-full: 9999px; - --padding-xs: 6px; - --padding-sm: 8px; - --padding-md: 10px; - --padding-lg: 12px; + --padding-xs: 4px; + --padding-sm: 6px; + --padding-md: 8px; + --padding-lg: 10px; --padding-xl: 16px; --padding-2xl: 2px; --padding-3xl: 2px; - --padding-long-xs: 12px; - --padding-long-sm: 14px; - --padding-long-md: 18px; - --padding-long-lg: 26px; - --padding-long-xl: 2px; - --padding-long-2xl: 2px; - --padding-long-3xl: 2px; - --gap-xs: 2px; - --gap-sm: 4px; - --gap-md: 6px; - --gap-lg: 8px; + --gap-sm: 3px; + --gap-md: 4px; + --gap-lg: 5px; --gap-xl: 10px; --gap-2xl: 12px; --gap-3xl: 14px; diff --git a/packages/ui-react/src/styles/recipes/item-size-recipe.ts b/packages/ui-react/src/styles/recipes/item-size-recipe.ts index 5dfeb15..02af392 100644 --- a/packages/ui-react/src/styles/recipes/item-size-recipe.ts +++ b/packages/ui-react/src/styles/recipes/item-size-recipe.ts @@ -27,14 +27,6 @@ export const itemSizeRecipe = createClassRecipe({ }, }, compoundVariants: [ - { size: "xs", isLongPadding: true, class: "px--long-xs" }, - { size: "sm", isLongPadding: true, class: "px--long-sm" }, - { size: "md", isLongPadding: true, class: "px--long-md" }, - { size: "lg", isLongPadding: true, class: "px--long-lg" }, - { size: "xl", isLongPadding: true, class: "px--long-xl" }, - { size: "2xl", isLongPadding: true, class: "px--long-2xl" }, - { size: "3xl", isLongPadding: true, class: "px--long-3xl" }, - // ================================================== { shape: "rounded", size: "xs", class: "rounded--xs" }, { shape: "rounded", size: "sm", class: "rounded--sm" }, { shape: "rounded", size: "md", class: "rounded--md" },