This commit is contained in:
2026-11-24 16:17:45 +08:00
parent a3dbdd7d11
commit ea56e4cabd
140 changed files with 1214 additions and 702 deletions
@@ -1,8 +0,0 @@
export const CheckIndicatorSvg = (props: React.SVGProps<SVGSVGElement>) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
<path
fill="currentColor"
d="M9 16.17L4.83 12l-1.42 1.41L9 19L21 7l-1.41-1.41z"
/>
</svg>
);
@@ -1,14 +0,0 @@
export const FileSvg = (props: React.SVGProps<SVGSVGElement>) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
<g
fill="none"
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="2"
>
<path d="M14 3v4a1 1 0 0 0 1 1h4" />
<path d="M17 21H7a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h7l5 5v11a2 2 0 0 1-2 2" />
</g>
</svg>
);
@@ -1,5 +1,5 @@
export const BoldSvg = (props: React.SVGProps<SVGSVGElement>) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
export const BoldSvg = () => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path
fill="none"
stroke="currentColor"
@@ -0,0 +1,8 @@
export const CheckIndicatorSvg = () => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path
fill="currentColor"
d="M9 16.17L4.83 12l-1.42 1.41L9 19L21 7l-1.41-1.41z"
/>
</svg>
);
@@ -0,0 +1,13 @@
export const CheckboxCheckedSvg = () => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
<path d="M0 0h32v32H0z" fill="none" />
<path
fill="currentColor"
d="M26 4H6a2 2 0 0 0-2 2v20a2 2 0 0 0 2 2h20a2 2 0 0 0 2-2V6a2 2 0 0 0-2-2M6 26V6h20v20Z"
/>
<path
fill="currentColor"
d="m14 21.5l-5-4.96L10.59 15L14 18.35L21.41 11L23 12.58z"
/>
</svg>
);
@@ -0,0 +1,9 @@
export const CheckboxUncheckedSvg = () => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
<path d="M0 0h32v32H0z" fill="none" />
<path
fill="currentColor"
d="M26 4H6a2 2 0 0 0-2 2v20a2 2 0 0 0 2 2h20a2 2 0 0 0 2-2V6a2 2 0 0 0-2-2M6 26V6h20v20Z"
/>
</svg>
);
@@ -1,5 +1,5 @@
export const ChevronRightSvg = (props: React.SVGProps<SVGSVGElement>) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
export const ChevronRightSvg = () => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path
fill="none"
stroke="currentColor"
@@ -1,5 +1,5 @@
export const CloseSvg = (props: React.SVGProps<SVGSVGElement>) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
export const CloseSvg = () => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path
fill="none"
stroke="currentColor"
@@ -1,5 +1,5 @@
export const CutSvg = (props: React.SVGProps<SVGSVGElement>) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
export const CutSvg = () => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path
fill="currentColor"
d="M12.14 9.342L7.37 2.329a.75.75 0 1 0-1.24.844l5.13 7.545l-2.395 3.743a4 4 0 1 0 1.178.943l2.135-3.337l2.065 3.036a4 4 0 1 0 1.261-.813l-2.447-3.597l.002-.002zM4.5 18a2.5 2.5 0 1 1 5 0a2.5 2.5 0 0 1-5 0m10 0a2.5 2.5 0 1 1 5 0a2.5 2.5 0 0 1-5 0m-.562-8.684l3.943-6.162a.75.75 0 1 0-1.263-.808L13.02 7.968z"
@@ -1,5 +1,5 @@
export const DownloadSvg = (props: React.SVGProps<SVGSVGElement>) => (
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 24 24" {...props}>
export const DownloadSvg = () => (
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 24 24">
<path fill="currentColor" d="M20,15.5c.55,0,1,.45,1,1v2c0,1.66-1.34,3-3,3H6c-1.66,0-3-1.34-3-3v-2c0-.55.45-1,1-1s1,.45,1,1v2c0,.55.45,1,1,1h12c.55,0,1-.45,1-1v-2c0-.55.45-1,1-1M12,2.5c.55,0,1,.45,1,1v9.59l3.29-3.29c.4-.38,1.03-.37,1.41.02.37.39.37,1,0,1.39l-5,5s-.06.06-.09.08l.09-.08c-.18.18-.42.28-.67.29h-.03s-.03,0-.03,0h-.05s.09,0,.09,0c-.22,0-.44-.07-.62-.21-.03-.02-.06-.05-.09-.08l-5-5c-.38-.4-.37-1.03.02-1.41.39-.37,1-.37,1.39,0l3.29,3.29V3.5c0-.55.45-1,1-1" />
</svg>
@@ -1,5 +1,5 @@
export const KeySvg = (props: React.SVGProps<SVGSVGElement>) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
export const KeySvg = () => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path
fill="currentColor"
d="M7 14q-.825 0-1.412-.587T5 12t.588-1.412T7 10t1.413.588T9 12t-.587 1.413T7 14m0 4q-2.5 0-4.25-1.75T1 12t1.75-4.25T7 6q1.675 0 3.038.825T12.2 9h8.375q.2 0 .388.075t.337.225l2 2q.15.15.212.325t.063.375t-.063.375t-.212.325l-3.175 3.175q-.125.125-.3.2t-.35.1t-.35-.025t-.325-.175L17.5 15l-1.425 1.075q-.125.1-.275.15t-.3.05t-.313-.05t-.287-.15L13.375 15H12.2q-.8 1.35-2.163 2.175T7 18m0-2q1.4 0 2.463-.85T10.875 13H14l1.45 1.025v.013v-.013L17.5 12.5l1.775 1.375L21.15 12h-.012h.012l-1-1v-.012V11h-9.275q-.35-1.3-1.412-2.15T7 8Q5.35 8 4.175 9.175T3 12t1.175 2.825T7 16"
@@ -1,5 +1,5 @@
export const MeshSvg = (props: React.SVGProps<SVGSVGElement>) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
export const MeshSvg = () => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<g
fill="none"
stroke="currentColor"
@@ -1,5 +1,5 @@
export const MoonSvg = (props: React.SVGProps<SVGSVGElement>) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
export const MoonSvg = () => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path
fill="none"
stroke="currentColor"
@@ -1,5 +1,5 @@
export const PasteSvg = (props: React.SVGProps<SVGSVGElement>) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
export const PasteSvg = () => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path
fill="currentColor"
d="M12.753 2c1.158 0 2.111.875 2.234 2h1.763a2.25 2.25 0 0 1 2.245 2.096L19 6.25a.75.75 0 0 1-.647.742L18.249 7a.75.75 0 0 1-.742-.647L17.5 6.25a.75.75 0 0 0-.648-.743L16.75 5.5h-2.132a2.24 2.24 0 0 1-1.865.993H9.247a2.24 2.24 0 0 1-1.865-.992L5.25 5.5a.75.75 0 0 0-.743.648L4.5 6.25v13.505c0 .38.282.693.648.743l.102.007h3a.75.75 0 0 1 .743.647l.007.102a.75.75 0 0 1-.75.75h-3a2.25 2.25 0 0 1-2.245-2.095L3 19.755V6.25a2.25 2.25 0 0 1 2.096-2.245L5.25 4h1.763a2.247 2.247 0 0 1 2.234-2zm5.997 6a2.25 2.25 0 0 1 2.245 2.096l.005.154v9.5a2.25 2.25 0 0 1-2.096 2.245L18.75 22h-6.5a2.25 2.25 0 0 1-2.245-2.096L10 19.75v-9.5a2.25 2.25 0 0 1 2.096-2.245L12.25 8zm0 1.5h-6.5a.75.75 0 0 0-.743.648l-.007.102v9.5c0 .38.282.694.648.743l.102.007h6.5a.75.75 0 0 0 .743-.648l.007-.102v-9.5a.75.75 0 0 0-.648-.743zm-5.997-6H9.247a.747.747 0 0 0 0 1.493h3.506a.747.747 0 1 0 0-1.493"
@@ -0,0 +1,9 @@
export const RadioboxCheckedSvg = () => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
<path
fill="currentColor"
d="M16 2a14 14 0 1 0 14 14A14 14 0 0 0 16 2m0 26a12 12 0 1 1 12-12a12 12 0 0 1-12 12"
></path>
<path fill="currentColor" d="M16 10a6 6 0 1 0 6 6a6 6 0 0 0-6-6"></path>
</svg>
);
@@ -0,0 +1,8 @@
export const RadioboxUncheckedSvg = () => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
<path
fill="currentColor"
d="M16 2a14 14 0 1 0 14 14A14 14 0 0 0 16 2m0 26a12 12 0 1 1 12-12a12 12 0 0 1-12 12"
></path>
</svg>
);
@@ -1,5 +1,5 @@
export const RulerSvg = (props: React.SVGProps<SVGSVGElement>) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
export const RulerSvg = () => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path
fill="none"
stroke="currentColor"
@@ -1,5 +1,5 @@
export const SearchSvg = (props: React.SVGProps<SVGSVGElement>) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
export const SearchSvg = () => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path
fill="currentColor"
d="M9.5 16q-2.725 0-4.612-1.888T3 9.5t1.888-4.612T9.5 3t4.613 1.888T16 9.5q0 1.1-.35 2.075T14.7 13.3l5.6 5.6q.275.275.275.7t-.275.7t-.7.275t-.7-.275l-5.6-5.6q-.75.6-1.725.95T9.5 16m0-2q1.875 0 3.188-1.312T14 9.5t-1.312-3.187T9.5 5T6.313 6.313T5 9.5t1.313 3.188T9.5 14"
@@ -1,5 +1,5 @@
export const SettingSvg = (props: React.SVGProps<SVGSVGElement>) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
export const SettingSvg = () => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path
fill="currentColor"
fillRule="evenodd"
@@ -1,6 +1,6 @@
export const SpinnerSvg = (props: React.SVGProps<SVGSVGElement>) => {
export const SpinnerSvg = () => {
return (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path
fill="currentColor"
d="M12,1A11,11,0,1,0,23,12,11,11,0,0,0,12,1Zm0,19a8,8,0,1,1,8-8A8,8,0,0,1,12,20Z"
@@ -1,5 +1,5 @@
export const SunSvg = (props: React.SVGProps<SVGSVGElement>) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
export const SunSvg = () => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path
fill="none"
stroke="currentColor"
@@ -1,6 +1,6 @@
export const UserSvg = (props: React.SVGProps<SVGSVGElement>) => {
export const UserSvg = () => {
return (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path
fill="none"
stroke="currentColor"
@@ -1,5 +1,5 @@
export const VolumeHighSvg = (props: React.SVGProps<SVGSVGElement>) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
export const VolumeHighSvg = () => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path
fill="none"
stroke="currentColor"
@@ -1,5 +1,5 @@
export const VolumeLowSvg = (props: React.SVGProps<SVGSVGElement>) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
export const VolumeLowSvg = () => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" >
<path
fill="none"
stroke="currentColor"
@@ -1,5 +1,5 @@
export const VolumeMuteSvg = (props: React.SVGProps<SVGSVGElement>) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
export const VolumeMuteSvg = () => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" >
<path
fill="none"
stroke="currentColor"
@@ -4,7 +4,7 @@ import {
import { mergeClass } from "../../styles/utils/merge-class";
import { displayRecipe } from "../../styles/recipes/display-recipe";
import { dotSizeRecipe } from "../../styles/recipes/dot-size-recipe";
import type { CommonProps } from "../../utils/CommonProps";
import type { CommonProps } from "../../utils/common-props";
import { brandRecipe } from "../../styles/recipes/brand-recipe";
export type BadgeProps = CommonProps & {
@@ -1,12 +1,13 @@
import React, { useState, type CSSProperties, type ReactNode } from "react";
import { SpinnerSvg } from "../../assets/svg/SpinnerSvg";
import React, { type CSSProperties, type ReactNode } from "react";
import { SpinnerSvg } from "../../assets/svg/spinner-svg";
import { Icon } from "../icon/icon";
import { displayRecipe } from "../../styles/recipes/display-recipe";
import { itemSizeRecipe } from "../../styles/recipes/item-size-recipe";
import { variantRecipe } from "../../styles/recipes/variant-recipe";
import { brandRecipe } from "../../styles/recipes/brand-recipe";
import type { CommonProps } from "../../utils/CommonProps";
import type { CommonProps } from "../../utils/common-props";
import { mergeClass } from "../../styles/utils/merge-class";
import { containerRecipe } from "../../styles/recipes/container-recipe";
// 缺个 loading 动画,暂时没想好怎么做,后面发现样本再抄
export type ButtonProps = CommonProps & {
@@ -61,8 +62,9 @@ export const Button = (props: ButtonProps) => {
// ====================================================================================
const rootClass = mergeClass(
containerRecipe({ hasFocusOutline: true }),
displayRecipe(),
itemSizeRecipe({ size, shape, isIconOnly}),
itemSizeRecipe({ size, shape, isIconOnly, isLongPadding: "true" }),
variantRecipe({ variant, isDisabled }),
brandRecipe({ brand: brandFinal }),
className,
@@ -0,0 +1,6 @@
import type { CommonProps } from "../../utils/common-props";
export type CheckboxInputGroupProps = CommonProps & {
name?: string;
selectedList?: string[];
};
@@ -0,0 +1,109 @@
import { useRef, useState, type ChangeEvent, type MouseEvent } from "react";
import type { CommonProps } from "../../utils/common-props";
import { mergeClass } from "../../styles/utils/merge-class";
import { inlineSizeRecipe } from "../../styles/recipes/inline-size-recipe";
import { useMergeRefs } from "@floating-ui/react";
import { containerRecipe } from "../../styles/recipes/container-recipe";
import { downGradeSize } from "../../utils/down-grade-size";
import { Icon } from "../icon/icon";
import { CheckboxCheckedSvg } from "../../assets/svg/checkbox-checked-svg";
import { CheckboxUncheckedSvg } from "../../assets/svg/checkbox-unchecked-svg";
import { itemSizeRecipe } from "../../styles/recipes/item-size-recipe";
export type CheckboxInputProps = CommonProps & {
size?: "sm" | "md";
name: string;
value: string;
checked?: boolean;
defaultChecked?: boolean;
onChange?: (nextChecked: boolean) => void;
asBoxOnly?: boolean;
};
/**
* Use CheckboxInput instead, this just a piece of Component.
*
* Same name will get like interest=coding&interest=music
*/
export const CheckboxInput = (props: CheckboxInputProps) => {
const {
size: propSize = "sm",
name,
value,
checked: controlledChecked,
defaultChecked = false,
onChange,
ref,
className = "",
children,
asBoxOnly = false,
} = props;
const size = asBoxOnly ? downGradeSize(propSize, 1) : propSize;
const downSize = asBoxOnly ? downGradeSize(size, 1) : size;
const [checked, setChecked] = useState<boolean>(
controlledChecked ?? defaultChecked,
);
const inputRef = useRef<HTMLInputElement>(null);
const rootClass = mergeClass(
containerRecipe(),
itemSizeRecipe({
size,
isIconOnly: asBoxOnly === true,
}),
className,
);
const boxClass = mergeClass(
inlineSizeRecipe({
size: downSize,
shape: "rounded",
isIconOnly: asBoxOnly === true,
}),
);
const inputClass = mergeClass("sr-only");
// =============================================================
// 将 input 的 onClick 范围扩大至整个容器元素
function clickHandler(e: MouseEvent<HTMLDivElement>) {
// 防止 input 和 div 重叠,响应两次 onClick
e.stopPropagation();
// 记住前面没有 on
inputRef?.current?.click();
}
function changeHandler(e: ChangeEvent<HTMLInputElement>) {
// 触发重新渲染,受控模式下,即使 set 也改变不了状态
setChecked(e.target.checked);
// 执行受控回调函数,向上层传递新值,或许上层组件会拒绝变更从 prop 传入的 checked
onChange?.(e.target.checked);
}
return (
<div className={rootClass} onClick={clickHandler}>
<div className={boxClass}>
<input
name={name}
value={value}
type="checkbox"
ref={useMergeRefs([inputRef, ref])}
// 绑定 state 第1/2步,从 state 中获取数据
checked={checked}
// 绑定 state 第2/2步,本元素被键盘点击后,将新值写回 state
onChange={changeHandler}
className={inputClass}
/>
<Icon size={downSize}>
{checked ? <CheckboxCheckedSvg /> : <CheckboxUncheckedSvg />}
</Icon>
{!asBoxOnly && children}
</div>
</div>
);
};
CheckboxInput.displayName = "CheckboxInput";
@@ -1,73 +0,0 @@
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";
checked?: boolean;
defaultChecked?: boolean;
onChange?: (nextChecked: boolean) => void;
};
export const Checkbox = (props: CheckboxProps) => {
const {
size = "sm",
checked: controlledChecked,
defaultChecked = false,
onChange,
ref,
className = "",
} = props;
const [checked, setChecked] = useState<boolean>(
controlledChecked ?? defaultChecked,
);
const inputRef = useRef<HTMLInputElement>(null);
const rootClass = mergeClass(
inlineSizeRecipe({ size, shape: "rounded", isIconOnly: true }),
className,
);
const inputClass = mergeClass("sr-only");
const indicatorClass = mergeClass(
inlineSizeRecipe({ size, shape: "rounded" }),
);
// 将 input 的 onClick 范围扩大至整个容器元素
function clickHandler(e: MouseEvent<HTMLDivElement>) {
// 防止 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>
);
};
Checkbox.displayName = "Checkbox";
+17 -6
View File
@@ -1,6 +1,5 @@
import { Slot } from "../../utils/slot";
import type { CommonProps } from "../../utils/CommonProps";
import type { CommonProps } from "../../utils/common-props";
import { inlineSizeRecipe } from "../../styles/recipes/inline-size-recipe";
import { mergeClass } from "../../styles/utils/merge-class";
@@ -8,16 +7,28 @@ export type IconProps = CommonProps & {
size?: "xs" | "sm" | "md" | "lg" | "xl" | "2xl" | "3xl";
shape?: "square" | "circle" | "rounded";
} & {
onClick?: (e?: React.MouseEvent) => void
onClick?: (e?: React.MouseEvent) => void;
};
export const Icon = (props: IconProps) => {
const { size = "md", className = "", children, shape = "square", ref, ...rest } = props;
const {
size = "md",
className = "",
children,
shape = "square",
ref,
...rest
} = props;
const rootClass = mergeClass(inlineSizeRecipe({ size, shape, isIconOnly: "true" }), className);
const rootClass = mergeClass(
inlineSizeRecipe({ size, shape, isIconOnly: "true" }),
className,
);
return (
<Slot {...rest} ref={ref} className={rootClass}>{children}</Slot>
<Slot {...rest} ref={ref} className={rootClass}>
{children}
</Slot>
);
};
@@ -2,23 +2,28 @@ import { useMenuFloatingContext } from "./use-menu-floating-context";
import { useListItem, useMergeRefs } from "@floating-ui/react";
import { useMenuContext } from "./use-menu-context";
import React, {
useEffect,
useId,
useRef,
useState,
type CSSProperties,
type MouseEvent,
} from "react";
import { Icon } from "../icon/icon";
import { Checkbox } from "../checkbox/checkbox";
import { CheckboxInput } from "../checkbox-input/checkbox-input";
import { displayRecipe } from "../../styles/recipes/display-recipe";
import { mergeClass } from "../../styles/utils/merge-class";
import { itemSizeRecipe } from "../../styles/recipes/item-size-recipe";
import { variantRecipe } from "../../styles/recipes/variant-recipe";
import type { CommonProps } from "../../utils/CommonProps";
import type { CommonProps } from "../../utils/common-props";
import { containerRecipe } from "../../styles/recipes/container-recipe";
import { inlineSizeRecipe } from "../../styles/recipes/inline-size-recipe";
export type MenuCheckItemProps = CommonProps & {
size?: "sm" | "md";
iconSvg?: React.ReactNode;
hotkey?: string;
// =================
checked?: boolean;
defaultChecked?: boolean;
onChange?: (nextChecked: boolean) => void;
@@ -26,13 +31,14 @@ export type MenuCheckItemProps = CommonProps & {
export const MenuCheckItem = (props: MenuCheckItemProps) => {
const {
size: propSize = "sm",
checked: controlledChecked,
defaultChecked = false,
// ==============================
onChange,
ref,
children,
iconSvg = <div></div>,
iconSvg,
isDisabled = false,
hotkey,
style,
@@ -44,15 +50,36 @@ export const MenuCheckItem = (props: MenuCheckItemProps) => {
);
const inputRef = useRef<HTMLInputElement>(null);
// =============== Menu Context ====================================================================
// =============== Menu Context Consumer ====================================================================
const menuContext = useMenuContext();
const { size = "sm" } = menuContext!;
const size = menuContext?.size ?? propSize;
// =============== Menu Floating Context ====================================================================
const stateRegistry = menuContext?.stateRegistry;
const stateKeyRef = useRef<string>(null);
useEffect(() => {
stateKeyRef.current = useId();
stateRegistry?.insertKey(stateKeyRef.current);
stateRegistry?.updateValue(stateKeyRef.current, {
hasIcon: iconSvg != undefined,
hasCheckbox: true,
hasIndicator: false,
});
return () => {
// 清除注册表里的数据,永远不会有无用 key
stateRegistry?.deleteEntry(stateKeyRef.current!);
};
}, []);
// 后面渲染要用到
const stateResult = menuContext?.stateResult;
// =============== Menu Floating Context Consumer ====================================================================
const menuFloatingContext = useMenuFloatingContext();
const { getItemProps, activeIndex, isOpen } = menuFloatingContext!;
const { getItemProps, activeIndex } = menuFloatingContext!;
const item = useListItem();
const isActive = item.index === activeIndex;
@@ -71,9 +98,17 @@ export const MenuCheckItem = (props: MenuCheckItemProps) => {
...style,
};
const startSectionClass = displayRecipe();
const startSectionClass = mergeClass([
displayRecipe(),
inlineSizeRecipe({ size }),
]);
const endSectionClass = displayRecipe();
const endSectionClass = mergeClass([
displayRecipe(),
inlineSizeRecipe({ size }),
]);
// =============== Event ==========================================
// 将 Checkbox 的 onClick 范围扩大至整个容器元素
function clickHandler(e: MouseEvent<HTMLDivElement>) {
@@ -83,7 +118,6 @@ export const MenuCheckItem = (props: MenuCheckItemProps) => {
inputRef?.current?.click();
}
// 绑定 state 第2/3步,本元素被键盘点击后,将新值写回 state
function changeHandler(nextChecked: boolean) {
// 触发重新渲染,受控模式下,即使 set 也改变不了状态
setChecked(nextChecked);
@@ -105,27 +139,38 @@ export const MenuCheckItem = (props: MenuCheckItemProps) => {
onClick: clickHandler,
})}
>
<Checkbox
<CheckboxInput
name=""
value=""
ref={useMergeRefs([inputRef, ref])}
size={size}
defaultChecked={checked}
// 绑定 state 第1/3步,从 state 中获取数据
// 绑定 state 第1/2步,从 state 中获取数据
checked={checked}
// 绑定 state 第2/3步,本元素被键盘点击后,将新值写回 state
// 绑定 state 第2/2步,本元素被键盘点击后,将新值写回 state
onChange={changeHandler}
/>
<div className={startSectionClass}>
<Icon size={size}>{iconSvg}</Icon>
{stateResult?.hasIcon && iconSvg ? (
<Icon size={size}>{iconSvg}</Icon>
) : (
<Icon size={size}>
<div></div>
</Icon>
)}
{children}
</div>
<div className={endSectionClass}>
{hotkey && <div>{hotkey}</div>}
{/* 占位 */}
<Icon size={size}>
<div></div>
</Icon>
{stateResult?.hasIndicator && (
<Icon size={size}>
<div></div>
</Icon>
)}
</div>
</button>
);
@@ -1,15 +1,16 @@
import type { ComponentPropsWithoutRef, CSSProperties } from "react";
import type { CSSProperties } from "react";
import {
FloatingFocusManager,
FloatingList,
FloatingPortal,
useMergeRefs,
} from "@floating-ui/react";
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 { useThemeContext } from "../theme/use-theme-context";
import type { CommonProps } from "../../utils/CommonProps";
import type { CommonProps } from "../../utils/common-props";
import { brandRecipe } from "../../styles/recipes/brand-recipe";
export type MenuDropdownProps = CommonProps & {
@@ -17,30 +18,41 @@ export type MenuDropdownProps = CommonProps & {
};
export const MenuDropdown = (props: MenuDropdownProps) => {
const { modal = true, className = "", children, style, ...rest } = props;
const {
modal = false,
className = "",
children,
style,
ref,
...rest
} = props;
// =========== Menu Context Consumer ===================================================================================================
const { size } = useMenuContext()!;
// =========== Menu Floating Context Consumer ===================================================================================================
const {
refs,
elementsRef,
context,
floatingStyles,
getFloatingProps,
isNested,
isOpen,
} = useMenuFloatingContext()!;
// ==============================================================================================================
// =========== Theme Context Consumer ===================================================================================================
const { themeBaseClass, themeClass } = useThemeContext()!;
// ===============================================================================================================
// ============== Style =================================================================================================
const rootClass = mergeClass(
containerRecipe({
boxShadow: true,
rounded: size == "sm" ? 4 : 8,
padding: size == "sm" ? 4 : 8,
rounded: size == "sm" ? 4 : size == "md" ? 8 : undefined,
padding: size == "sm" ? 4 : size == "md" ? 8 : undefined,
background: "base",
hasFocusOutline: "false",
}),
@@ -1,18 +1,20 @@
import { useMenuFloatingContext } from "./use-menu-floating-context";
import { useListItem, useMergeRefs } from "@floating-ui/react";
import React, { type CSSProperties } from "react";
import React, { useEffect, useId, useRef, type CSSProperties } from "react";
import { Icon } from "../icon/icon";
import { ChevronRightSvg } from "../../assets/svg/ChevronRightSvg";
import { ChevronRightSvg } from "../../assets/svg/chevron-right-svg";
import { useMenuTriggerContext } from "./use-menu-trigger-context";
import { useMenuContext } from "./use-menu-context";
import { mergeClass } from "../../styles/utils/merge-class";
import { displayRecipe } from "../../styles/recipes/display-recipe";
import { itemSizeRecipe } from "../../styles/recipes/item-size-recipe";
import { variantRecipe } from "../../styles/recipes/variant-recipe";
import type { CommonProps } from "../../utils/CommonProps";
import type { CommonProps } from "../../utils/common-props";
import { containerRecipe } from "../../styles/recipes/container-recipe";
import { inlineSizeRecipe } from "../../styles/recipes/inline-size-recipe";
export type MenuItemProps = CommonProps & {
size?: "sm" | "md";
onClick?: (e?: React.MouseEvent) => void;
iconSvg?: React.ReactNode;
indicatorSvg?: React.ReactNode;
@@ -21,9 +23,10 @@ export type MenuItemProps = CommonProps & {
export const MenuItem = (props: MenuItemProps) => {
const {
size: propSize = "sm",
ref,
children,
iconSvg = <div></div>,
iconSvg,
indicatorSvg = <ChevronRightSvg />,
onClick,
style,
@@ -32,11 +35,39 @@ export const MenuItem = (props: MenuItemProps) => {
...rest
} = props;
// =============== Menu Context ====================================================================
// =============== Menu Trigger Context Consumer ====================================================================
const { size = "sm" } = useMenuContext()!;
const menuTriggerContext = useMenuTriggerContext();
const isTrigger = !!menuTriggerContext;
// =============== Menu Floating Context ====================================================================
// =============== Menu Context Consumer ====================================================================
const menuContext = useMenuContext();
const size = menuContext?.size ?? propSize;
const stateRegistry = menuContext?.stateRegistry;
const stateKeyRef = useRef<string>(null);
// 注册 state
useEffect(() => {
stateKeyRef.current = useId();
stateRegistry?.insertKey(stateKeyRef.current);
stateRegistry?.updateValue(stateKeyRef.current, {
hasIcon: iconSvg != undefined,
hasCheckbox: false,
hasIndicator: isTrigger,
});
return () => {
// 清除注册表里的数据,永远不会有无用 key 和脏数据
stateRegistry?.deleteEntry(stateKeyRef.current!);
};
}, []);
// 后面渲染要用到
const stateResult = menuContext?.stateResult;
// =============== Menu Floating Context Consumer ====================================================================
const menuFloatingContext = useMenuFloatingContext();
const { getItemProps, activeIndex, isOpen } = menuFloatingContext!;
@@ -44,12 +75,6 @@ export const MenuItem = (props: MenuItemProps) => {
const item = useListItem();
const isActive = item.index === activeIndex;
// =============== Menu Trigger Context ====================================================================
const menuTriggerContext = useMenuTriggerContext();
const isTrigger = !!menuTriggerContext;
// ================== Style =================================================================
const rootClass = mergeClass(
@@ -64,9 +89,15 @@ export const MenuItem = (props: MenuItemProps) => {
...style,
};
const startSectionClass = displayRecipe();
const startSectionClass = mergeClass([
displayRecipe(),
inlineSizeRecipe({ size }),
]);
const endSectionClass = displayRecipe();
const endSectionClass = mergeClass([
displayRecipe(),
inlineSizeRecipe({ size }),
]);
return (
<button
@@ -84,20 +115,29 @@ export const MenuItem = (props: MenuItemProps) => {
})}
>
<div className={startSectionClass}>
<Icon size={size}>{iconSvg}</Icon>
{stateResult?.hasIcon && iconSvg ? (
<Icon size={size}>{iconSvg}</Icon>
) : (
<Icon size={size}>
<div></div>
</Icon>
)}
{children}
</div>
<div className={endSectionClass}>
{hotkey && <div>{hotkey}</div>}
{isTrigger && isOpen && (
<Icon size={size} style={{ rotate: "90deg" }}>
{indicatorSvg}
</Icon>
)}
{isTrigger && !isOpen && <Icon size={size}>{indicatorSvg}</Icon>}
{!isTrigger && (
// 占位
{stateResult?.hasIndicator && (
<Icon size={size}>
<div></div>
</Icon>
@@ -1,10 +1,11 @@
import { useEffect, useRef, useState } from "react";
import type { CommonProps } from "../../utils/CommonProps";
import { MenuRadioItemGroupContext } from "./use-menu-radio-item-group-context";
import type { CommonProps } from "../../utils/common-props";
import { MenuRadioItemGroupContext } from "../radiobox-input/use-radiobox-input-group-context";
import { useMenuContext } from "./use-menu-context";
export type MenuRadioItemGroupProps = CommonProps & {
size?: "sm" | "md";
name: string;
defaultSelectName?: string;
onChange?: (nextSelect?: string) => void;
};
@@ -18,7 +19,7 @@ export const MenuRadioItemGroup = (props: MenuRadioItemGroupProps) => {
...rest
} = props;
const [selectName, setSelectName] = useState<string>(defaultSelectName);
const [selectValue, setSelectValue] = useState<string>(defaultSelectName);
const { size: menuSize } = useMenuContext()!;
@@ -26,7 +27,7 @@ export const MenuRadioItemGroup = (props: MenuRadioItemGroupProps) => {
return (
<MenuRadioItemGroupContext
value={{ size: currentSize, selectName, setSelectName }}
value={{ size: currentSize, selectValue, setSelectValue}}
>
<div {...rest}>{children}</div>
</MenuRadioItemGroupContext>
@@ -1,20 +1,38 @@
import { useRef } from "react";
import type { CommonProps } from "../../utils/CommonProps";
import { useMenuRadioItemGroupContext } from "./use-menu-radio-item-group-context";
import type { CommonProps } from "../../utils/common-props";
import { useMenuRadioItemGroupContext } from "../radiobox-input/use-radiobox-input-group-context";
export type MenuRadioItemProps = CommonProps & {
name: string;
iconSvg?: React.ReactNode;
hotkey?: string;
onChange?: (preCheck?: boolean, nextCheck?: boolean) => void
size?: "sm" | "md";
name?: string;
value?: string;
iconSvg?: React.ReactNode;
hotkey?: string;
// ===============
onChange?: (nextSelect?: boolean) => void;
};
export const MenuRadioItem = (props: MenuRadioItemProps) => {
const { name, children, className, hotkey, onChange, ref, iconSvg, style } = props
const {
size: propSize = "sm",
name,
value,
iconSvg,
hotkey,
onChange,
ref,
children,
className,
style,
} = props;
const { selectName, setSelectName } = useMenuRadioItemGroupContext()!
const isSelect = name === selectName
return <div></div>
}
// ========= Radio Group Context Cunsumer =====================================================
const { selectValue, setSelectValue } = useMenuRadioItemGroupContext()!;
const isSelect = value === selectValue;
const inputRef = useRef<HTMLInputElement>(null);
// =========================================================
return <div></div>;
};
@@ -1,25 +1,24 @@
import type { ComponentPropsWithoutRef } from "react";
import { useMenuFloatingContext } from "./use-menu-floating-context";
import { Slot } from "../../utils/slot";
import { MenuTriggerContext } from "./use-menu-trigger-context";
import type { CommonProps } from "../../utils/common-props";
import { containerRecipe } from "../../styles/recipes/container-recipe";
export type MenuTriggerProps = ComponentPropsWithoutRef<"div">;
export type MenuTriggerProps = CommonProps;
export const MenuTrigger = (props: MenuTriggerProps) => {
const { children } = props;
const menuFloatingContext = useMenuFloatingContext();
const { refs, setIsOpen, getReferenceProps } = menuFloatingContext!;
const { refs, getReferenceProps } = useMenuFloatingContext()!;
return (
<MenuTriggerContext.Provider value={{ isTrigger: true }}>
<Slot className={"reset"} ref={refs.setReference} {...getReferenceProps(
{ onClick: () => { setIsOpen(true) } }
)}>
<Slot
ref={refs.setReference}
{...getReferenceProps()}
>
{children}
</Slot>
</MenuTriggerContext.Provider>
)
);
};
+83 -30
View File
@@ -1,54 +1,107 @@
import {
FloatingNode,
FloatingTree,
} from "@floating-ui/react";
import type { ComponentPropsWithoutRef } from "react";
import { FloatingNode, FloatingTree } from "@floating-ui/react";
import { useLayoutEffect, type ComponentPropsWithoutRef } from "react";
import { useMenuFloating } from "./use-menu-floating";
import { MenuFloatingContext } from "./use-menu-floating-context";
import { MenuContext } from "./use-menu-context";
import { MenuContext, useMenuContext } from "./use-menu-context";
import { mergeClass } from "../../styles/utils/merge-class";
import { useStateRegistry } from "../../utils/use-state-registry";
export type MenuProps = ComponentPropsWithoutRef<"div"> & {
size?: "sm" | "md";
};
export const Menu = (props: MenuProps) => {
const { size = "sm", className = "", children, ...rest } = props;
const { size: propSize = "sm", className = "", children, ...rest } = props;
// ============= parent Menu Context Cunsumer ==================================
const parentMenuContext = useMenuContext();
const size = parentMenuContext?.size ?? propSize;
// ============= Menu Floating Provider ==================================
const menuFloatingContextValue = useMenuFloating();
const { isNested, nodeId } = menuFloatingContextValue!
const { isNested, nodeId } = menuFloatingContextValue!;
// ==========================================================================================
// ============= Menu Context Provider ==================================
const rootClass = mergeClass("position--relative",className)
const stateRegistry = useStateRegistry<{
hasIcon: boolean;
hasCheckbox: boolean;
hasIndicator: boolean;
}>((registry) => {
let result: {
hasIcon: boolean;
hasCheckbox: boolean;
hasIndicator: boolean;
} = { hasIcon: false, hasCheckbox: false, hasIndicator: false };
// ==========================================================================================
registry.forEach((v) => {
if (v?.hasIcon) {
result.hasIcon = true;
}
if (v?.hasCheckbox) {
result.hasCheckbox = true;
}
if (v?.hasIndicator) {
result.hasIndicator = true;
}
});
if (!isNested) {
return (
<MenuContext.Provider value={{ size }}>
<MenuFloatingContext.Provider value={menuFloatingContextValue}>
<FloatingTree>
<FloatingNode id={nodeId}>
<div {...rest} className={rootClass}>
{children}
</div>
</FloatingNode>
</FloatingTree>
</MenuFloatingContext.Provider>
</MenuContext.Provider>
);
}
return result;
});
return (
<MenuContext.Provider value={{ size }}>
<MenuFloatingContext.Provider value={menuFloatingContextValue}>
// ============== Style ============================================================================
const rootClass = mergeClass("position--relative", className);
// ============== Collect Childrens States ============================================================================
let stateResult:
| {
hasIcon: boolean;
hasCheckbox: boolean;
hasIndicator: boolean;
}
| undefined = undefined;
useLayoutEffect(() => {
if (stateRegistry.checkAllReady()) {
stateResult = stateRegistry.getResult();
}
});
// ======= Condition Return ======================================================
let returnStatement;
// 如果值为 undefined,则 return 空值, 也就是不渲染任何东西,但实际渲染函数还是执行了的
if (stateResult) {
if (!isNested) {
returnStatement = (
<FloatingTree>
<FloatingNode id={nodeId}>
<div {...rest} className={rootClass}>
{children}
</div>
</FloatingNode>
</FloatingTree>
);
} else {
returnStatement = (
<FloatingNode id={nodeId}>
<div {...rest} className={rootClass} >
<div {...rest} className={rootClass}>
{children}
</div>
</FloatingNode>
);
}
}
return (
<MenuContext.Provider value={{ size, stateRegistry, stateResult }}>
<MenuFloatingContext.Provider value={menuFloatingContextValue}>
{returnStatement}
</MenuFloatingContext.Provider>
</MenuContext.Provider>
);
@@ -1,6 +1,21 @@
import { createContext, useContext } from "react";
import type { useStateRegistry } from "../../utils/use-state-registry";
export type MenuContextValue = { size?: "sm" | "md" } | null;
export type MenuContextValue = {
size?: "sm" | "md";
stateRegistry?: ReturnType<
typeof useStateRegistry<{
hasIcon: boolean;
hasCheckbox: boolean;
hasIndicator: boolean;
}>
>;
stateResult?: {
hasIcon: boolean;
hasCheckbox: boolean;
hasIndicator: boolean;
};
} | null;
export const MenuContext = createContext<MenuContextValue>(null);
@@ -44,20 +44,20 @@ export function useMenuFloating() {
});
const hover = useHover(context, {
// 仅子菜单启用 hover
enabled: isNested,
delay: { open: 75 },
// 模拟 click, 鼠标即使移出了安全区,也不关闭 1s=1000ms
// 10分钟 = 6,000,000
delay: { open: 75, close: 6000000 },
// 必须开启,否则鼠标进入子菜单就关闭
handleClose: safePolygon({ blockPointerEvents: true }),
});
const click = useClick(context, {
event: "mousedown",
toggle: !isNested,
ignoreMouse: isNested,
});
const click = useClick(context, {});
const role = useRole(context, { role: "menu" });
const dismiss = useDismiss(context, { bubbles: true });
const dismiss = useDismiss(context);
const listNav = useListNavigation(context, {
listRef: elementsRef,
@@ -86,6 +86,6 @@ export function useMenuFloating() {
setSelectedIndex,
activeIndex,
isOpen,
setIsOpen
setIsOpen,
};
}
@@ -1,13 +0,0 @@
import { createContext, useContext } from "react";
export type MenuRadioItemGroupContextValue = {
size: "sm" | "md"
selectName: string;
setSelectName: React.Dispatch<React.SetStateAction<string>>;
} | null;
export const MenuRadioItemGroupContext = createContext<MenuRadioItemGroupContextValue>(null)
export function useMenuRadioItemGroupContext() {
return useContext(MenuRadioItemGroupContext)
}
@@ -1,5 +1,5 @@
import type { CSSProperties } from "react";
import type { CommonProps } from "../../utils/CommonProps";
import type { CommonProps } from "../../utils/common-props";
import { usePopoverContext } from "./use-popover-context";
import {
FloatingArrow,
@@ -23,6 +23,7 @@ export const PopoverDropdown = (props: PopoverDropdownProps) => {
context,
arrowRef,
isMounted,
isOpen,
transitionStyles,
arrowWidth,
arrowHeight,
@@ -57,14 +58,15 @@ export const PopoverDropdown = (props: PopoverDropdownProps) => {
return (
<>
{isMounted && (
{isOpen && (
<FloatingPortal>
<FloatingFocusManager context={context} modal={false}>
<div
className={rootClass}
{...rest}
ref={refs.setFloating}
className={rootClass}
style={rootStyle}
{...getFloatingProps({ ...rest })}
{...getFloatingProps()}
>
{children}
<FloatingArrow
@@ -1,17 +1,17 @@
import type { CommonProps } from "../../utils/CommonProps"
import type { CommonProps } from "../../utils/common-props";
import { Slot } from "../../utils/slot";
import { usePopoverContext } from "./use-popover-context";
export type PopoverTriggerProps = CommonProps
export type PopoverTriggerProps = CommonProps;
export const PopoverTrigger = (props: PopoverTriggerProps) => {
const { children, ...rest } = props;
const { children, ...rest } = props;
const { refs, getReferenceProps, setIsOpen, isOpen } = usePopoverContext()!
const { refs, getReferenceProps } = usePopoverContext()!;
return <Slot ref={refs.setReference} {...getReferenceProps(
{ ...rest, onClick: () => { setIsOpen(!isOpen) } }
)}>
{children}
return (
<Slot ref={refs.setReference} {...getReferenceProps()}>
{children}
</Slot>
}
);
};
@@ -1,16 +1,16 @@
import type { CommonProps } from "../../utils/CommonProps"
import { PopoverContext } from "./use-popover-context"
import { usePopoverFloating } from "./use-popover-floating"
import type { CommonProps } from "../../utils/common-props";
import { PopoverContext } from "./use-popover-context";
import { usePopoverFloating } from "./use-popover-floating";
export type PopoverProps = CommonProps & {
}
export type PopoverProps = CommonProps & {};
export const Popover = (props: PopoverProps) => {
const { children } = props
const popoverFloatingContext = usePopoverFloating()
const { children } = props;
const popoverFloatingContext = usePopoverFloating();
return <PopoverContext.Provider value={popoverFloatingContext}>
<div>{children}</div>
return (
<PopoverContext.Provider value={popoverFloatingContext}>
<div>{children}</div>
</PopoverContext.Provider>
}
);
};
@@ -1,83 +1,111 @@
import { arrow, autoUpdate, flip, offset, shift, useClick, useDismiss, useFloating, useInteractions, useRole, useTransitionStyles } from "@floating-ui/react";
import {
arrow,
autoUpdate,
flip,
offset,
shift,
useClick,
useDismiss,
useFloating,
useInteractions,
useRole,
useTransitionStyles,
} from "@floating-ui/react";
import { useMemo, useRef, useState } from "react";
export function usePopoverFloating() {
const [isOpen, setIsOpen] = useState(false);
const arrowRef = useRef(null);
const [isOpen, setIsOpen] = useState(false);
const arrowRef = useRef(null);
// 拉扯 svg 图像,让三角变得,更尖更长,或者更宽更扁
const arrowWidth = 16
const arrowHeight = 8
// 拉扯 svg 图像,让三角变得,更尖更长,或者更宽更扁
const arrowWidth = 16;
const arrowHeight = 8;
const { refs, floatingStyles, context, middlewareData } = useFloating({
placement: "top-start",
open: isOpen,
onOpenChange: setIsOpen,
whileElementsMounted: autoUpdate,
middleware: [
offset(arrowHeight),
flip({ padding: 8 }),
// 偏移完成后,再留 8px 空白,而不是紧贴着父容器
shift({ padding: 8 }),
arrow({
element: arrowRef,
}),
],
});
const { refs, floatingStyles, context, middlewareData } = useFloating({
placement: "top-start",
open: isOpen,
onOpenChange: setIsOpen,
whileElementsMounted: autoUpdate,
middleware: [
offset(arrowHeight),
flip({ padding: 8 }),
// 偏移完成后,再留 8px 空白,而不是紧贴着父容器
shift({ padding: 8 }),
arrow({
element: arrowRef,
}),
],
});
// 这两个是未经 useFloating 变换前的坐标
// 也就是 arrow 悬浮,左对齐 floating,两者水平中线重合
const arrowX = middlewareData.arrow?.x ?? 0;
const arrowY = middlewareData.arrow?.y ?? 0;
// 这两个是未经 useFloating 变换前的坐标
// 也就是 arrow 悬浮,左对齐 floating,两者水平中线重合
const arrowX = middlewareData.arrow?.x ?? 0;
const arrowY = middlewareData.arrow?.y ?? 0;
// 变换中心的起始坐标就是 floating 的左上角
const transformX = arrowX + arrowWidth / 2;
const transformY = arrowY + arrowHeight;
// 变换中心的起始坐标就是 floating 的左上角
const transformX = arrowX + arrowWidth / 2;
const transformY = arrowY + arrowHeight;
// style 用于内容框,而不是 FloatingArrow
const { isMounted, styles: transitionStyles } = useTransitionStyles(context, {
duration: 150,
initial: {
opacity: 0,
transform: "scale(0)"
},
close: {
opacity: 0
},
common: ({ side }) => ({
transitionTimingFunction: "ease",
transformOrigin: {
top: `${transformX}px calc(100% + ${arrowHeight}px)`,
bottom: `${transformX}px ${-arrowHeight}px`,
left: `calc(100% + ${arrowHeight}px) ${transformY}px`,
right: `${-arrowHeight}px ${transformY}px`
}[side]
})
})
// style 用于内容框,而不是 FloatingArrow
const { isMounted, styles: transitionStyles } = useTransitionStyles(context, {
duration: 150,
initial: {
opacity: 0,
transform: "scale(0)",
},
close: {
opacity: 0,
},
common: ({ side }) => ({
transitionTimingFunction: "ease",
transformOrigin: {
top: `${transformX}px calc(100% + ${arrowHeight}px)`,
bottom: `${transformX}px ${-arrowHeight}px`,
left: `calc(100% + ${arrowHeight}px) ${transformY}px`,
right: `${-arrowHeight}px ${transformY}px`,
}[side],
}),
});
const click = useClick(context);
const dismiss = useDismiss(context);
const role = useRole(context);
const click = useClick(context);
const dismiss = useDismiss(context);
const role = useRole(context);
const { getReferenceProps, getFloatingProps } = useInteractions([
click,
dismiss,
role,
]);
const { getReferenceProps, getFloatingProps } = useInteractions([
click,
dismiss,
role,
]);
return useMemo(() => ({
isOpen,
setIsOpen,
/* only use at {isMounted && <FloatingPortal/>} */
isMounted,
arrowRef,
arrowHeight,
arrowWidth,
refs,
floatingStyles,
transitionStyles,
context,
getReferenceProps,
getFloatingProps
}), [isOpen, setIsOpen, isMounted, refs, arrowHeight, arrowWidth, arrowRef, floatingStyles, transitionStyles, context, getReferenceProps, getFloatingProps])
}
return useMemo(
() => ({
isOpen,
setIsOpen,
/* only use at {isMounted && <FloatingPortal/>} */
isMounted,
arrowRef,
arrowHeight,
arrowWidth,
refs,
floatingStyles,
transitionStyles,
context,
getReferenceProps,
getFloatingProps,
}),
[
isOpen,
setIsOpen,
isMounted,
refs,
arrowHeight,
arrowWidth,
arrowRef,
floatingStyles,
transitionStyles,
context,
getReferenceProps,
getFloatingProps,
],
);
}
@@ -0,0 +1,37 @@
import { useEffect, useState } from "react";
import type { CommonProps } from "../../utils/common-props";
import { RadioboxInputGroupContext } from "./use-radiobox-input-group-context";
export type RadioboxInputGroupProps = CommonProps & {
size?: "sm" | "md";
name: string;
defaultSelectedValue?: string;
onChange?: (nextSelected?: string) => void;
canNoneSelected?: boolean;
};
export const RadioboxInputGroup = (props: RadioboxInputGroupProps) => {
const {
size = "sm",
defaultSelectedValue,
name,
onChange,
canNoneSelected = false,
children,
} = props;
const [selectedValue, setSelectedValue] = useState<string | undefined>(
defaultSelectedValue,
);
useEffect(() => {
onChange?.(selectedValue);
}, [selectedValue]);
return (
<RadioboxInputGroupContext.Provider
value={{ size, selectedValue, setSelectedValue, name, canNoneSelected }}
>
{children}
</RadioboxInputGroupContext.Provider>
);
};
@@ -0,0 +1,122 @@
import { useRef, useState, type ChangeEvent, type MouseEvent } from "react";
import { useMergeRefs } from "@floating-ui/react";
import type { CommonProps } from "../../utils/common-props";
import { downGradeSize } from "../../utils/down-grade-size";
import { mergeClass } from "../../styles/utils/merge-class";
import { containerRecipe } from "../../styles/recipes/container-recipe";
import { inlineSizeRecipe } from "../../styles/recipes/inline-size-recipe";
import { Icon } from "../icon/icon";
import { RadioboxCheckedSvg } from "../../assets/svg/radiobox-checked-svg";
import { RadioboxUncheckedSvg } from "../../assets/svg/radiobox-unchecked-svg";
import { itemSizeRecipe } from "../../styles/recipes/item-size-recipe";
import { useRadioboxInputGroupContext } from "./use-radiobox-input-group-context";
import { displayRecipe } from "../../styles/recipes/display-recipe";
export type RadioboxInputProps = CommonProps & {
value: string;
size?: "sm" | "md";
checked?: boolean;
defaultChecked?: boolean;
onChange?: (nextChecked: boolean) => void;
asBoxOnly?: boolean;
};
/**
* Use RadioboxInput instead, this just a piece of Component.
*
* a group is define by the same name.
*/
export const RadioboxInput = (props: RadioboxInputProps) => {
const {
size: propSize = "sm",
value,
onChange,
ref,
className = "",
children,
asBoxOnly = false,
} = props;
// ======== Group Context =============================================
const {
size: groupSize,
name: groupName,
selectedValue,
setSelectedValue,
canNoneSelected,
} = useRadioboxInputGroupContext()!;
const currentSize = groupSize ?? propSize;
const size = asBoxOnly ? downGradeSize(currentSize, 1) : currentSize;
const downSize = asBoxOnly ? downGradeSize(size, 1) : size;
const inputRef = useRef<HTMLInputElement>(null);
const isSelected = selectedValue === value ? true : false;
// ========= Style =========================================
const rootClass = mergeClass(
containerRecipe(),
itemSizeRecipe({ size, isIconOnly: asBoxOnly === true }),
className,
);
const boxClass = mergeClass(
inlineSizeRecipe({
size: downSize,
shape: "circle",
isIconOnly: asBoxOnly === true,
}),
);
// sr-only 1个像素都没有,更不可能被鼠标点击
const inputClass = mergeClass(displayRecipe({ screenReaderOnly: "hide" }));
// ========== Event ===================================================
// 将 input 的 onClick 范围扩大至整个容器元素
function clickHandler(e: MouseEvent<HTMLDivElement>) {
e.stopPropagation();
// 这里点击 input
inputRef.current?.click();
}
function changeHandler(e: ChangeEvent<HTMLInputElement>) {
if (isSelected == true && canNoneSelected == true) {
setSelectedValue?.(undefined);
} else if (isSelected == true && canNoneSelected == false) {
// do nothing, bucause repeat selected
// isSelected will reset input.checked value
} else {
setSelectedValue?.(value);
onChange?.(e.target.checked);
}
}
// =============================================================
return (
<div className={rootClass} onClick={clickHandler}>
<div className={boxClass}>
<input
name={groupName}
value={value}
type="radio"
ref={useMergeRefs([inputRef, ref])}
// 绑定 state 第1/2步,从 state 中获取数据
checked={isSelected}
// 绑定 state 第2/2步,本元素被键盘点击后,将新值写回 state
onChange={changeHandler}
className={inputClass}
/>
<Icon size={downSize}>
{isSelected ? <RadioboxCheckedSvg /> : <RadioboxUncheckedSvg />}
</Icon>
{!asBoxOnly && children}
</div>
</div>
);
};
RadioboxInput.displayName = "RadioboxInput";
@@ -0,0 +1,16 @@
import { createContext, useContext } from "react";
export type RadioboxInputGroupContextValue = {
size?: "sm" | "md";
selectedValue?: string;
setSelectedValue?: React.Dispatch<React.SetStateAction<string | undefined>>;
name?: string;
canNoneSelected?: boolean;
} | null;
export const RadioboxInputGroupContext =
createContext<RadioboxInputGroupContextValue>(null);
export function useRadioboxInputGroupContext() {
return useContext(RadioboxInputGroupContext);
}
+2 -2
View File
@@ -1,11 +1,11 @@
import { Icon } from "../icon/icon";
import { CloseSvg } from "../../assets/svg/CloseSvg";
import { CloseSvg } from "../../assets/svg/close-svg";
import { displayRecipe } from "../../styles/recipes/display-recipe";
import { itemSizeRecipe } from "../../styles/recipes/item-size-recipe";
import { brandRecipe } from "../../styles/recipes/brand-recipe";
import { variantRecipe } from "../../styles/recipes/variant-recipe";
import type { CommonProps } from "../../utils/CommonProps";
import type { CommonProps } from "../../utils/common-props";
import { useState, type ReactNode } from "react";
import { mergeClass } from "../../styles/utils/merge-class";
@@ -1,6 +1,6 @@
import { useState } from "react";
import type { CommonProps } from "../../utils/CommonProps";
import type { CommonProps } from "../../utils/common-props";
import { ThemeContext } from "./use-theme-context";
import { mergeClass } from "../../styles/utils/merge-class";
+1
View File
@@ -16,6 +16,7 @@
@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';
+30 -25
View File
@@ -1,8 +1,11 @@
export * from './utils/state-registry.ts';
export * from './utils/use-state-registry.ts';
export * from './utils/slot.tsx';
export * from './utils/radiobox.tsx';
export * from './utils/merge-props.ts';
export * from './utils/CommonProps.ts';
export * from './utils/camelToKebab.ts';
export * from './utils/down-grade-size.ts';
export * from './utils/common-props.ts';
export * from './components/checkbox-input/checkbox-input.tsx';
export * from './utils/camel-to-kebab.ts';
export * from './styles/utils/merge-class.ts';
export * from './styles/utils/create-class-recipe.ts';
export * from './styles/recipes/variant-recipe.ts';
@@ -22,7 +25,7 @@ export * from './components/popover/popover.tsx';
export * from './components/popover/popover-trigger.tsx';
export * from './components/popover/popover-dropdown.tsx';
export * from './components/menu/use-menu-trigger-context.ts';
export * from './components/menu/use-menu-radio-item-group-context.ts';
export * from './components/radiobox-input/use-radiobox-input-group-context.ts';
export * from './components/menu/use-menu-floating.ts';
export * from './components/menu/use-menu-floating-context.ts';
export * from './components/menu/use-menu-context.ts';
@@ -34,26 +37,28 @@ export * from './components/menu/menu-item.tsx';
export * from './components/menu/menu-dropdown.tsx';
export * from './components/menu/menu-check-item.tsx';
export * from './components/icon/icon.tsx';
export * from './components/checkbox/checkbox.tsx';
export * from './components/button/button.tsx';
export * from './components/badge/badge.tsx';
export * from './assets/svg/VolumeMuteSvg.tsx';
export * from './assets/svg/VolumeLowSvg.tsx';
export * from './assets/svg/VolumeHighSvg.tsx';
export * from './assets/svg/UserSvg.tsx';
export * from './assets/svg/SunSvg.tsx';
export * from './assets/svg/SpinnerSvg.tsx';
export * from './assets/svg/SettingSvg.tsx';
export * from './assets/svg/SearchSvg.tsx';
export * from './assets/svg/Ruler.tsx';
export * from './assets/svg/PasteSvg.tsx';
export * from './assets/svg/MoonSvg.tsx';
export * from './assets/svg/MeshSvg.tsx';
export * from './assets/svg/KeySvg.tsx';
export * from './assets/svg/FileSvg.tsx';
export * from './assets/svg/DownloadSvg.tsx';
export * from './assets/svg/CutSvg.tsx';
export * from './assets/svg/CloseSvg.tsx';
export * from './assets/svg/ChevronRightSvg.tsx';
export * from './assets/svg/CheckIndicatorSvg.tsx';
export * from './assets/svg/BoldSvg.tsx';
export * from './assets/svg/volume-mute-svg.tsx';
export * from './assets/svg/volume-low-svg.tsx';
export * from './assets/svg/volume-high-svg.tsx';
export * from './assets/svg/user-svg.tsx';
export * from './assets/svg/sun-svg.tsx';
export * from './assets/svg/spinner-svg.tsx';
export * from './assets/svg/setting-svg.tsx';
export * from './assets/svg/search-svg.tsx';
export * from './assets/svg/ruler-svg.tsx';
export * from './assets/svg/radiobox-unchecked-svg.tsx';
export * from './assets/svg/radiobox-checked-svg.tsx';
export * from './assets/svg/paste-svg.tsx';
export * from './assets/svg/moon-svg.tsx';
export * from './assets/svg/mesh-svg.tsx';
export * from './assets/svg/key-svg.tsx';
export * from './assets/svg/download-svg.tsx';
export * from './assets/svg/cut-svg.tsx';
export * from './assets/svg/close-svg.tsx';
export * from './assets/svg/chevron-right-svg.tsx';
export * from './assets/svg/checkbox-unchecked-svg.tsx';
export * from './assets/svg/checkbox-checked-svg.tsx';
export * from './assets/svg/check-indicator-svg.tsx';
export * from './assets/svg/bold-svg.tsx';
@@ -1,25 +1,57 @@
@layer utility {
.h--inline-xs {
height: calc(var(--font-size-xs) * var(--line-height));
& svg {
height: calc(var(--font-size-xs) * var(--line-height));
width: calc(var(--font-size-xs) * var(--line-height));
}
}
.h--inline-sm {
height: calc(var(--font-size-sm) * var(--line-height));
& svg {
height: calc(var(--font-size-sm) * var(--line-height));
width: calc(var(--font-size-sm) * var(--line-height));
}
}
.h--inline-md {
height: calc(var(--font-size-md) * var(--line-height));
& svg {
height: calc(var(--font-size-md) * var(--line-height));
width: calc(var(--font-size-md) * var(--line-height));
}
}
.h--inline-lg {
height: calc(var(--font-size-lg) * var(--line-height));
& svg {
height: calc(var(--font-size-lg) * var(--line-height));
width: calc(var(--font-size-lg) * var(--line-height));
}
}
.h--inline-xl {
height: calc(var(--font-size-xl) * var(--line-height));
& svg {
height: calc(var(--font-size-xl) * var(--line-height));
width: calc(var(--font-size-xl) * var(--line-height));
}
}
.h--inline-2xl {
height: calc(var(--font-size-2xl) * var(--line-height));
& svg {
height: calc(var(--font-size-2xl) * var(--line-height));
width: calc(var(--font-size-2xl) * var(--line-height));
}
}
}
.h--inline-3xl {
height: calc(var(--font-size-3xl) * var(--line-height));
& svg {
height: calc(var(--font-size-3xl) * var(--line-height));
width: calc(var(--font-size-3xl) * var(--line-height));
}
}
}
@@ -1,23 +1,51 @@
@layer utility {
.h--item-xs {
height: var(--height-item-xs);
& svg {
height: calc(var(--font-size-xs) * var(--line-height));
width: calc(var(--font-size-xs) * var(--line-height));
}
}
.h--item-sm {
height: var(--height-item-sm);
& svg {
height: calc(var(--font-size-sm) * var(--line-height));
width: calc(var(--font-size-sm) * var(--line-height));
}
}
.h--item-md {
height: var(--height-item-md);
& svg {
height: calc(var(--font-size-md) * var(--line-height));
width: calc(var(--font-size-md) * var(--line-height));
}
}
.h--item-lg {
height: var(--height-item-lg);
& svg {
height: calc(var(--font-size-lg) * var(--line-height));
width: calc(var(--font-size-lg) * var(--line-height));
}
}
.h--item-xl {
height: var(--height-item-xl);
& svg {
height: calc(var(--font-size-xl) * var(--line-height));
width: calc(var(--font-size-xl) * var(--line-height));
}
}
.h--item-2xl {
height: var(--height-item-2xl);
& svg {
height: calc(var(--font-size-2xl) * var(--line-height));
width: calc(var(--font-size-2xl) * var(--line-height));
}
}
.h--item-3xl {
height: var(--height-item-3xl);
& svg {
height: calc(var(--font-size-3xl) * var(--line-height));
width: calc(var(--font-size-3xl) * var(--line-height));
}
}
}
@@ -0,0 +1,9 @@
@layer utility {
.opacity--0 {
opacity: 0;
}
.opacity--1 {
opacity: 1;
}
}
+19 -13
View File
@@ -1,16 +1,22 @@
@layer utility {
.outline--none {
outline: none;
/* 要注意,这不是不可焦距,只是不显示 outline 而已,但仍可能显示 hover */
.outline--none {
outline: none;
}
/* 仅启用键盘焦距,显示 outline,但仍可能显示其他的,例如,hover */
.outline {
/* 先移除所有显示 */
&:focus {
outline: none;
}
.outline {
&:focus-visible {
outline: 3px solid var(--base-focus-outline-color);
outline-offset: 1px;
}
&:focus:not(:focus-visible) {
outline: none;
}
/* 键盘点击,显示 */
&:focus-visible {
outline: 3px solid var(--base-focus-outline-color);
outline-offset: 1px;
}
}
/* 鼠标点击后,不显示 */
&:focus:not(:focus-visible) {
outline: none;
}
}
}
@@ -0,0 +1,23 @@
@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);
}
.px--long-3xl {
padding-inline: var(--padding-long-3xl);
}
}
@@ -333,7 +333,7 @@
--font-size-xs: 12px;
--font-size-sm: 13px;
--font-size-md: 14px;
--font-size-lg: 16px;
--font-size-lg: 15px;
--font-size-xl: 16px;
--font-size-2xl: 24px;
--font-size-3xl: 30px;
@@ -374,13 +374,23 @@
--radius-full: 9999px;
--padding-xs: 4px;
--padding-sm: 6px;
--padding-md: 8px;
--padding-lg: 10px;
--padding-sm: 5px;
--padding-md: 6px;
--padding-lg: 7px;
--padding-xl: 16px;
--padding-2xl: 2px;
--padding-3xl: 2px;
--times-of-padding-long: 2;
--padding-long-xs: calc(var(--padding-xs) * var(--times-of-padding-long));
--padding-long-sm: calc(var(--padding-sm) * var(--times-of-padding-long));
--padding-long-md: calc(var(--padding-md) * var(--times-of-padding-long));
--padding-long-lg: calc(var(--padding-lg) * var(--times-of-padding-long));
--padding-long-xl: calc(var(--padding-xl) * var(--times-of-padding-long));
--padding-long-2xl: calc(var(--padding-2xl) * var(--times-of-padding-long));
--padding-long-3xl: calc(var(--padding-3xl) * var(--times-of-padding-long));
--gap-xs: 2px;
--gap-sm: 3px;
--gap-md: 4px;
@@ -17,7 +17,7 @@ export const containerRecipe = createClassRecipe({
},
padding: {
4: "px--4 py--4",
8: "px--8 py--8",
8: "px--8 py--8",
12: "px--12 py--12",
16: "px--16 py--16",
},
@@ -61,5 +61,9 @@ export const displayRecipe = createClassRecipe({
grab: "cursor--grab",
grabbing: "cursor--grabbing",
},
opacity: {
0: "opacity--0",
1: "opacity--1",
},
},
});
@@ -27,6 +27,13 @@ export const itemSizeRecipe = createClassRecipe({
},
},
compoundVariants: [
{ isLongPadding: true, size: "xs", class: "px--long-xs" },
{ isLongPadding: true, size: "sm", class: "px--long-sm" },
{ isLongPadding: true, size: "md", class: "px--long-md" },
{ isLongPadding: true, size: "lg", class: "px--long-lg" },
{ isLongPadding: true, size: "xl", class: "px--long-xl" },
{ isLongPadding: true, size: "2xl", class: "px--long-2xl" },
{ isLongPadding: true, size: "3xl", 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" },
@@ -34,7 +41,6 @@ export const itemSizeRecipe = createClassRecipe({
{ shape: "rounded", size: "xl", class: "rounded--xl" },
{ shape: "rounded", size: "2xl", class: "rounded--2xl" },
{ shape: "rounded", size: "3xl", class: "rounded--3xl" },
// ==================================================
{ isIconOnly: true, size: "xs", class: "w--item-xs px--none" },
{ isIconOnly: true, size: "sm", class: "w--item-sm px--none" },
{ isIconOnly: true, size: "md", class: "w--item-md px--none" },
@@ -0,0 +1,27 @@
type SizeType = ["xs", "sm", "md", "lg", "xl", "2xl", "3xl"];
/**
*
* @returns
* Return available value or undefind
*/
export function downGradeSize(
size: "xs" | "sm" | "md" | "lg" | "xl" | "2xl" | "3xl" | undefined,
downGrade: number = 1,
): "xs" | "sm" | "md" | "lg" | "xl" | "2xl" | "3xl" | undefined {
if (size == undefined) {
return undefined;
}
const sizeArray: SizeType = ["xs", "sm", "md", "lg", "xl", "2xl", "3xl"];
const index = sizeArray.findIndex((v) => v == size);
const downIndex = index - downGrade;
if (downIndex < 0) {
return undefined;
}
return sizeArray[downIndex];
}
@@ -1,33 +0,0 @@
export class StateRegistry<V> {
constructor(excuteCallback: (registry: Map<string, V | null>) => boolean) {
this._excuteCallback = excuteCallback;
}
_excuteCallback: (registry: Map<string, V | null>) => boolean;
_registryMap = new Map<string, V | null>();
insertKey = (key: string) => {
this._registryMap.set(key, null);
};
updateValue = (key: string, value: V) => {
this._registryMap.set(key, value);
};
deleteEntry = (key: string) => {
this._registryMap.delete(key);
};
execute = () => {
return this._excuteCallback(this._registryMap);
};
checkAllReady = () => {
for (const i of this._registryMap.values()) {
i == null;
return false;
}
return true;
};
}
@@ -0,0 +1,55 @@
import { useState } from "react";
export function useStateRegistry<V>(
resultCallback?: (registry: Map<string, V | undefined>) => V,
) {
const [registry, setRegistry] = useState<Map<string, V | undefined>>(
new Map(),
);
const insertKey = (key: string) => {
setRegistry((preMap) => new Map(preMap.set(key, undefined)));
};
const updateValue = (key: string, value: V) => {
setRegistry((preMap) => new Map(preMap.set(key, value)));
};
const deleteEntry = (key: string) => {
setRegistry((preMap) => {
const newMap = new Map(preMap);
newMap.delete(key);
return newMap;
});
};
/**
* @returns Checking all key-value entry, if have a value of entry is undefined, return false.
*/
const checkAllReady = () => {
for (const i of registry.values()) {
i == undefined;
return false;
}
return true;
};
/**
* @returns If not pass resultCallback, just return undefined
*/
const getResult = () => {
if (resultCallback == undefined) {
return undefined;
} else {
return resultCallback(registry);
}
};
return {
insertKey,
updateValue,
deleteEntry,
checkAllReady,
getResult,
};
}