This commit is contained in:
2026-11-22 04:09:28 +08:00
parent 4bce6dd47e
commit a8b160aa45
14 changed files with 304 additions and 176 deletions
+2
View File
@@ -1,6 +1,7 @@
import { ButtonGallery } from "./button-gallery"; import { ButtonGallery } from "./button-gallery";
import { Theme } from "../src/components/theme/theme"; import { Theme } from "../src/components/theme/theme";
import { PopoverGallery } from "./popover-gallery"; import { PopoverGallery } from "./popover-gallery";
import { MenuGallery } from "./menu-gallery";
export function App() { export function App() {
return ( return (
@@ -8,6 +9,7 @@ export function App() {
<Theme> <Theme>
<ButtonGallery /> <ButtonGallery />
<PopoverGallery /> <PopoverGallery />
<MenuGallery />
</Theme> </Theme>
</div> </div>
); );
@@ -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 (
<OW>
<IW>
<Menu>
<MenuTrigger>
<Button size="sm" variant="ghost">
文件
</Button>
</MenuTrigger>
<MenuDropdown>
<MenuItem>Open File...</MenuItem>
<MenuItem>Open Folder...</MenuItem>
<Menu>
<MenuTrigger>
<MenuItem>Preferences</MenuItem>
</MenuTrigger>
<MenuDropdown>
<MenuItem>Profile</MenuItem>
<MenuItem>Settings</MenuItem>
<MenuCheckItem>Auto Lint</MenuCheckItem>
</MenuDropdown>
</Menu>
<Menu>
<MenuTrigger>
<MenuItem>Ruler</MenuItem>
</MenuTrigger>
<MenuDropdown>
<MenuRadioItemGroup defaultSelectName="hori">
<MenuRadioItem name="hori">Hori</MenuRadioItem>
<MenuRadioItem name="verti">Verti</MenuRadioItem>
<MenuRadioItem name="both">Hoth</MenuRadioItem>
</MenuRadioItemGroup>
</MenuDropdown>
</Menu>
</MenuDropdown>
</Menu>
</IW>
</OW>
);
};
@@ -8,6 +8,7 @@ import { brandRecipe } from "../../styles/recipes/brand-recipe";
import type { CommonProps } from "../../utils/CommonProps"; import type { CommonProps } from "../../utils/CommonProps";
import { mergeClass } from "../../styles/utils/merge-class"; import { mergeClass } from "../../styles/utils/merge-class";
// 缺个 loading 动画,暂时没想好怎么做,后面发现样本再抄
export type ButtonProps = CommonProps & { export type ButtonProps = CommonProps & {
size?: "xs" | "sm" | "md" | "lg"; size?: "xs" | "sm" | "md" | "lg";
variant?: "filled" | "outline" | "subtle" | "ghost"; variant?: "filled" | "outline" | "subtle" | "ghost";
@@ -54,11 +55,6 @@ export const Button = (props: ButtonProps) => {
...rest ...rest
} = props; } = props;
const [innerIsLoading, setInnerIsIsLoading] = useState<{
state: boolean;
class: string;
}>({ state: false, class: "" });
const brandFinal = const brandFinal =
brand != undefined ? brand : variant == "filled" ? "info" : "default"; brand != undefined ? brand : variant == "filled" ? "info" : "default";
@@ -66,14 +62,13 @@ export const Button = (props: ButtonProps) => {
const rootClass = mergeClass( const rootClass = mergeClass(
displayRecipe(), displayRecipe(),
itemSizeRecipe({ size, shape, isIconOnly, isLongPadding: true }), itemSizeRecipe({ size, shape, isIconOnly}),
variantRecipe({ variant, isDisabled }), variantRecipe({ variant, isDisabled }),
brandRecipe({ brand: brandFinal }), brandRecipe({ brand: brandFinal }),
className, className,
); );
let rootStyle: CSSProperties = { let rootStyle: CSSProperties = {
transitionDuration: "all 5s ease-in-out",
width: width, width: width,
...style, ...style,
}; };
@@ -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 { CheckIndicatorSvg } from "../../assets/svg/CheckIndicatorSvg";
import type { CommonProps } from "../../utils/CommonProps"; import type { CommonProps } from "../../utils/CommonProps";
import { mergeClass } from "../../styles/utils/merge-class"; import { mergeClass } from "../../styles/utils/merge-class";
import { inlineSizeRecipe } from "../../styles/recipes/inline-size-recipe"; import { inlineSizeRecipe } from "../../styles/recipes/inline-size-recipe";
import { useMergeRefs } from "@floating-ui/react";
export type CheckboxProps = CommonProps & { export type CheckboxProps = CommonProps & {
size?: "sm" | "md", size?: "sm" | "md";
defaultCheck?: boolean, checked?: boolean;
onChange?: (preCheck?: boolean, nextCheck?: boolean) => void, defaultChecked?: boolean;
controllerCheck?: boolean onChange?: (nextChecked: boolean) => void;
}; };
export const Checkbox = (props: CheckboxProps) => { 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<boolean>(defaultCheck) const [checked, setChecked] = useState<boolean>(
controlledChecked ?? defaultChecked,
);
const rootClass = mergeClass(inlineSizeRecipe({ size, shape: "rounded", isIconOnly: true }), className) const inputRef = useRef<HTMLInputElement>(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 indicatorClass = mergeClass(
const preValue = isCheck inlineSizeRecipe({ size, shape: "rounded" }),
setIsCheck(!isCheck) );
onChange?.(preValue, isCheck)
}
return <div className={rootClass} onClick={clickHandler} > // 将 input 的 onClick 范围扩大至整个容器元素
<input className={inputClass} type="checkbox" ref={ref} /> function clickHandler(e: MouseEvent<HTMLDivElement>) {
{isCheck && <CheckIndicatorSvg className={indicatorClass} />} // 防止 input 和 div 重叠,响应两次 onClick
e.stopPropagation();
// 记住前面没有 on
inputRef?.current?.click();
}
// 绑定 state 第2/3步,本元素被键盘点击后,将新值写回 state
function changeHandler(e: ChangeEvent<HTMLInputElement>) {
// 触发重新渲染,受控模式下,即使 set 也改变不了状态
setChecked(e.target.checked);
// 执行受控回调函数,向上层传递新值,或许上层组件会拒绝变更从 prop 传入的 checked
onChange?.(e.target.checked);
}
return (
<div className={rootClass} onClick={clickHandler}>
<input
className={inputClass}
type="checkbox"
ref={useMergeRefs([inputRef, ref])}
// 绑定 state 第1/3步,从 state 中获取数据
checked={checked}
// 绑定 state 第2/3步,本元素被键盘点击后,将新值写回 state
onChange={changeHandler}
/>
{checked && <CheckIndicatorSvg className={indicatorClass} />}
</div> </div>
} );
Checkbox.displayName = "Checkbox" };
Checkbox.displayName = "Checkbox";
@@ -1,8 +1,12 @@
import { useMenuFloatingContext } from "./use-menu-floating-context"; import { useMenuFloatingContext } from "./use-menu-floating-context";
import { useListItem, useMergeRefs } from "@floating-ui/react"; import { useListItem, useMergeRefs } from "@floating-ui/react";
import { useMenuContext } from "./use-menu-context"; 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 { Icon } from "../icon/icon";
import { Checkbox } from "../checkbox/checkbox"; import { Checkbox } from "../checkbox/checkbox";
import { displayRecipe } from "../../styles/recipes/display-recipe"; import { displayRecipe } from "../../styles/recipes/display-recipe";
@@ -13,74 +17,116 @@ import type { CommonProps } from "../../utils/CommonProps";
import { containerRecipe } from "../../styles/recipes/container-recipe"; import { containerRecipe } from "../../styles/recipes/container-recipe";
export type MenuCheckItemProps = CommonProps & { export type MenuCheckItemProps = CommonProps & {
iconSvg?: React.ReactNode; iconSvg?: React.ReactNode;
hotkey?: string; hotkey?: string;
check?: boolean checked?: boolean;
onChange?: (preCheck?: boolean, nextCheck?: boolean) => void defaultChecked?: boolean;
onChange?: (nextChecked: boolean) => void;
}; };
export const MenuCheckItem = (props: MenuCheckItemProps) => { export const MenuCheckItem = (props: MenuCheckItemProps) => {
const { ref, children, iconSvg = <div></div>, style, hotkey, isDisabled = false, onChange, ...rest } = props; const {
checked: controlledChecked,
defaultChecked = false,
// ==============================
onChange,
ref,
children,
iconSvg = <div></div>,
isDisabled = false,
hotkey,
style,
...rest
} = props;
const [isCheck, setIsCheck] = useState<boolean>(false) const [checked, setChecked] = useState<boolean>(
controlledChecked ?? defaultChecked,
);
const inputRef = useRef<HTMLInputElement>(null);
// =============== Menu Context ==================================================================== // =============== Menu Context ====================================================================
const menuContext = useMenuContext(); const menuContext = useMenuContext();
const { size = "sm" } = menuContext! const { size = "sm" } = menuContext!;
// =============== Menu Floating Context ==================================================================== // =============== Menu Floating Context ====================================================================
const menuFloatingContext = useMenuFloatingContext(); const menuFloatingContext = useMenuFloatingContext();
const { getItemProps, activeIndex, isOpen, } = menuFloatingContext! const { getItemProps, activeIndex, isOpen } = menuFloatingContext!;
const item = useListItem(); const item = useListItem();
const isActive = item.index === activeIndex; const isActive = item.index === activeIndex;
// ================== Style ================================================================= // ================== Style =================================================================
const rootClass = mergeClass(displayRecipe({ justifyContent: "between", }), containerRecipe({ hasFocusOutline: "false" }), const rootClass = mergeClass(
itemSizeRecipe({ size, shape: "rounded" }), displayRecipe({ justifyContent: "between" }),
variantRecipe({ variant: "ghost", isDisabled })) containerRecipe({ hasFocusOutline: "false" }),
itemSizeRecipe({ size, shape: "rounded" }),
variantRecipe({ variant: "ghost", isDisabled }),
);
const rootStyle: CSSProperties = { const rootStyle: CSSProperties = {
width: "100%", width: "100%",
...style ...style,
} };
const startSectionClass = displayRecipe() const startSectionClass = displayRecipe();
const endSectionClass = displayRecipe() const endSectionClass = displayRecipe();
function handleOnClick() { // 将 Checkbox 的 onClick 范围扩大至整个容器元素
const preCheck = isCheck function clickHandler(e: MouseEvent<HTMLDivElement>) {
setIsCheck(!isCheck) // 防止 input 和 div 重叠,响应两次 onClick
onChange?.(preCheck, isCheck) e.stopPropagation();
} // 前面没有 on
inputRef?.current?.click();
}
return <button {...rest} ref={useMergeRefs([ref, item.ref])} // 绑定 state 第2/3步,本元素被键盘点击后,将新值写回 state
className={rootClass} function changeHandler(nextChecked: boolean) {
role="menuitem" // 触发重新渲染,受控模式下,即使 set 也改变不了状态
tabIndex={isActive ? 0 : -1} setChecked(nextChecked);
disabled={isDisabled} // 执行受控回调函数,向上层传递新值,或许上层组件会拒绝变更从 prop 传入的 checked
{...getItemProps({ onChange?.(nextChecked);
style: rootStyle, }
onClick: handleOnClick
}) return (
} <button
{...rest}
// 此处不需要 mergeRefs,通过 onClick 已经连上了 ref
ref={item.ref}
className={rootClass}
role="menuitem"
tabIndex={isActive ? 0 : -1}
disabled={isDisabled}
{...getItemProps({
style: rootStyle,
onClick: clickHandler,
})}
> >
<Checkbox
ref={useMergeRefs([inputRef, ref])}
size={size}
defaultChecked={checked}
// 绑定 state 第1/3步,从 state 中获取数据
checked={checked}
// 绑定 state 第2/3步,本元素被键盘点击后,将新值写回 state
onChange={changeHandler}
/>
<Checkbox size={size} defaultCheck={isCheck} /> <div className={startSectionClass}>
<Icon size={size}>{iconSvg}</Icon>
<div className={startSectionClass}> {children}
<Icon size={size}>{iconSvg}</Icon> </div>
{children}
</div>
<div className={endSectionClass}>
{hotkey && <div>{hotkey}</div>}
<Icon size={size}><div></div></Icon>
</div>
</button >
<div className={endSectionClass}>
{hotkey && <div>{hotkey}</div>}
{/* 占位 */}
<Icon size={size}>
<div></div>
</Icon>
</div>
</button>
);
}; };
@@ -7,49 +7,71 @@ import {
import { useMenuFloatingContext } from "./use-menu-floating-context"; import { useMenuFloatingContext } from "./use-menu-floating-context";
import { useMenuContext } from "./use-menu-context"; import { useMenuContext } from "./use-menu-context";
import { mergeClass } from "../../styles/utils/merge-class"; 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 { useThemeContext } from "../theme/use-theme-context";
import type { CommonProps } from "../../utils/CommonProps";
import { brandRecipe } from "../../styles/recipes/brand-recipe";
export type MenuDropdownProps = CommonProps & {
export type MenuDropdownProps = ComponentPropsWithoutRef<"div"> & {
modal?: boolean; modal?: boolean;
}; };
export const MenuDropdown = (props: MenuDropdownProps) => { export const MenuDropdown = (props: MenuDropdownProps) => {
const { modal = true, className = "", children, ...rest } = props; const { modal = true, className = "", children, style, ...rest } = props;
const { size } = useMenuContext()! const { size } = useMenuContext()!;
const { refs, elementsRef, context, floatingStyles, getFloatingProps, isNested, isOpen } = useMenuFloatingContext()!; 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 rootClass = mergeClass(
containerRecipe({
const padding = size == "sm" ? "4px" : (size == "md" ? "8px" : undefined); boxShadow: true,
const borderRadius = size == "sm" ? "4px" : (size == "md" ? "8px" : undefined); rounded: size == "sm" ? 4 : 8,
padding: size == "sm" ? 4 : 8,
background: "base",
hasFocusOutline: "false",
}),
brandRecipe(),
themeBaseClass,
themeClass,
className,
);
const rootStyle: CSSProperties = { const rootStyle: CSSProperties = {
...floatingStyles, ...floatingStyles,
borderRadius: borderRadius, ...style,
padding: padding, };
// invisable the outline
outline: 0,
background: "var(--base-bg)"
} return (
<FloatingList elementsRef={elementsRef}>
return <FloatingList elementsRef={elementsRef}> {isOpen && (
{isOpen && <FloatingPortal > <FloatingPortal>
<FloatingFocusManager context={context} modal={modal} > <FloatingFocusManager context={context} modal={modal}>
<div {...rest} className={rootClass} style={rootStyle} ref={refs.setFloating} {...getFloatingProps()} > <div
{children} {...rest}
</div> className={rootClass}
</FloatingFocusManager> style={rootStyle}
</FloatingPortal> ref={refs.setFloating}
} {...getFloatingProps()}
</FloatingList > >
{children}
</div>
</FloatingFocusManager>
</FloatingPortal>
)}
</FloatingList>
);
}; };
@@ -97,6 +97,7 @@ export const MenuItem = (props: MenuItemProps) => {
)} )}
{isTrigger && !isOpen && <Icon size={size}>{indicatorSvg}</Icon>} {isTrigger && !isOpen && <Icon size={size}>{indicatorSvg}</Icon>}
{!isTrigger && ( {!isTrigger && (
// 占位
<Icon size={size}> <Icon size={size}>
<div></div> <div></div>
</Icon> </Icon>
@@ -1,24 +1,34 @@
import { useState } from "react"; import { useEffect, useRef, useState } from "react";
import type { CommonProps } from "../../utils/CommonProps"; import type { CommonProps } from "../../utils/CommonProps";
import { MenuRadioItemGroupContext } from "./use-menu-radio-item-group-context"; import { MenuRadioItemGroupContext } from "./use-menu-radio-item-group-context";
import { useMenuContext } from "./use-menu-context"; import { useMenuContext } from "./use-menu-context";
export type MenuRadioItemGroupProps = CommonProps & { export type MenuRadioItemGroupProps = CommonProps & {
size?: "sm" | "md"; size?: "sm" | "md";
defaultSelectName?: string defaultSelectName?: string;
onChange?: (nextSelect?: string) => void;
}; };
export const MenuRadioItemGroup = (props: MenuRadioItemGroupProps) => { export const MenuRadioItemGroup = (props: MenuRadioItemGroupProps) => {
const { size = "sm", defaultSelectName = "", ...rest } = props const {
size = "sm",
defaultSelectName = "",
children,
onChange,
...rest
} = props;
const [selectName, setSelectName] = useState<string>(defaultSelectName); const [selectName, setSelectName] = useState<string>(defaultSelectName);
const { size: menuSize } = useMenuContext()! const { size: menuSize } = useMenuContext()!;
const currentSize = menuSize ?? size const currentSize = menuSize ?? size;
return <MenuRadioItemGroupContext value={{ size: currentSize, selectName, setSelectName }}> return (
<div {...rest} ></div> <MenuRadioItemGroupContext
value={{ size: currentSize, selectName, setSelectName }}
>
<div {...rest}>{children}</div>
</MenuRadioItemGroupContext> </MenuRadioItemGroupContext>
);
} };
@@ -8,7 +8,6 @@ import {
} from "@floating-ui/react"; } from "@floating-ui/react";
import { useThemeContext } from "../theme/use-theme-context"; import { useThemeContext } from "../theme/use-theme-context";
import { mergeClass } from "../../styles/utils/merge-class"; import { mergeClass } from "../../styles/utils/merge-class";
import { variantRecipe } from "../../styles/recipes/variant-recipe";
import { brandRecipe } from "../../styles/recipes/brand-recipe"; import { brandRecipe } from "../../styles/recipes/brand-recipe";
import { containerRecipe } from "../../styles/recipes/container-recipe"; import { containerRecipe } from "../../styles/recipes/container-recipe";
-1
View File
@@ -16,7 +16,6 @@
@import './styles/css/position.css'; @import './styles/css/position.css';
@import './styles/css/pointer-events.css'; @import './styles/css/pointer-events.css';
@import './styles/css/padding.css'; @import './styles/css/padding.css';
@import './styles/css/padding-long.css';
@import './styles/css/overscroll-behavior.css'; @import './styles/css/overscroll-behavior.css';
@import './styles/css/overflow.css'; @import './styles/css/overflow.css';
@import './styles/css/outline.css'; @import './styles/css/outline.css';
+8 -9
View File
@@ -1,34 +1,33 @@
@layer utility { @layer utility {
.gap-none { .gap-none {
gap: none; gap: var(--gap-none);
} }
.gap--xs { .gap--xs {
gap: 2px; gap: var(--gap-xs);
} }
.gap--sm { .gap--sm {
gap: 4px; gap: var(--gap-sm);
} }
.gap--md { .gap--md {
gap: 6px; gap: var(--gap-md);
} }
.gap--lg { .gap--lg {
gap: 8px; gap: var(--gap-lg);
} }
.gap--xl { .gap--xl {
gap: 10px; gap: var(--gap-lg);
} }
.gap--2xl { .gap--2xl {
gap: 12px; gap: var(--gap-2xl);
} }
.gap--3xl { .gap--3xl {
gap: 14px; gap: var(--gap-3xl);
} }
} }
@@ -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);
}
}
@@ -358,8 +358,8 @@
--height-item-xs: 20px; --height-item-xs: 20px;
--height-item-sm: 24px; --height-item-sm: 24px;
--height-item-md: 28px; --height-item-md: 28px;
--height-item-lg: 34px; --height-item-lg: 32px;
--height-item-xl: 44px; --height-item-xl: 42px;
--height-item-2xl: 56px; --height-item-2xl: 56px;
--height-item-3xl: 64px; --height-item-3xl: 64px;
@@ -373,26 +373,18 @@
--radius-none: 0; --radius-none: 0;
--radius-full: 9999px; --radius-full: 9999px;
--padding-xs: 6px; --padding-xs: 4px;
--padding-sm: 8px; --padding-sm: 6px;
--padding-md: 10px; --padding-md: 8px;
--padding-lg: 12px; --padding-lg: 10px;
--padding-xl: 16px; --padding-xl: 16px;
--padding-2xl: 2px; --padding-2xl: 2px;
--padding-3xl: 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-xs: 2px;
--gap-sm: 4px; --gap-sm: 3px;
--gap-md: 6px; --gap-md: 4px;
--gap-lg: 8px; --gap-lg: 5px;
--gap-xl: 10px; --gap-xl: 10px;
--gap-2xl: 12px; --gap-2xl: 12px;
--gap-3xl: 14px; --gap-3xl: 14px;
@@ -27,14 +27,6 @@ export const itemSizeRecipe = createClassRecipe({
}, },
}, },
compoundVariants: [ 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: "xs", class: "rounded--xs" },
{ shape: "rounded", size: "sm", class: "rounded--sm" }, { shape: "rounded", size: "sm", class: "rounded--sm" },
{ shape: "rounded", size: "md", class: "rounded--md" }, { shape: "rounded", size: "md", class: "rounded--md" },