Skip to content

Commit 7073558

Browse files
committed
perf: add utility for background class checks
1 parent 8abbd80 commit 7073558

File tree

10 files changed

+74
-44
lines changed

10 files changed

+74
-44
lines changed

packages/react-ui/src/components/accordion/accordion-root.tsx

+5-34
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,24 @@
11
import { Composite } from "@floating-ui/react";
2-
import { useState, type KeyboardEvent } from "react";
2+
import { type KeyboardEvent, useState } from "react";
33
import { useControllableState } from "../../hooks";
44
import type { PrimitiveProps } from "../../primitives";
55
import { CompositeContext, type CompositeContextValue } from "../../primitives/composite/composite-context";
6+
import type { MultipleValueProps, SingleValueProps } from "../../shared/types";
67
import { tx } from "../../utils";
7-
import { AccordionContext, type AccordionBaseProps, type AccordionContextValue } from "./accordion-context";
8+
import { type AccordionBaseProps, AccordionContext, type AccordionContextValue } from "./accordion-context";
89

910
type AccordionSingleProps = {
1011
/**
1112
* 同时打开多个项目
1213
*/
1314
multiple: false;
1415

15-
/**
16-
* 受控值
17-
*/
18-
value?: string | number | null;
19-
20-
/**
21-
* 默认值
22-
*/
23-
defaultValue?: string | number | null;
24-
25-
/**
26-
* onChange 回调
27-
*/
28-
onChange?: (value: string | number | null) => void;
29-
3016
/**
3117
* 允许关闭内容, 当 `multiple` 为 `false` 时有效
3218
* @default false
3319
*/
3420
collapsible?: boolean;
35-
};
21+
} & Omit<SingleValueProps, "multiple">;
3622

3723
type AccordionMultipleProps = {
3824
/**
@@ -42,22 +28,7 @@ type AccordionMultipleProps = {
4228
multiple?: true;
4329

4430
collapsible?: never;
45-
46-
/**
47-
* 受控值
48-
*/
49-
value?: (string | number)[];
50-
51-
/**
52-
* 默认值
53-
*/
54-
defaultValue?: (string | number)[];
55-
56-
/**
57-
* onChange 回调
58-
*/
59-
onChange?: (value: (string | number)[]) => void;
60-
};
31+
} & Omit<MultipleValueProps, "multiple">;
6132

6233
export type AccordionRootProps = (AccordionMultipleProps | AccordionSingleProps) & AccordionBaseProps;
6334

packages/react-ui/src/components/avatar/avatar-root.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { isNumber } from "@resolid/utils";
22
import { type CSSProperties, useState } from "react";
33
import type { ImageLoadStatus } from "../../hooks";
44
import type { PrimitiveProps } from "../../primitives";
5+
import { hasBackgroundClass } from "../../shared/utils";
56
import { tx } from "../../utils";
67
import { AvatarContext, AvatarStatusContext } from "./avatar-context";
78
import { type AvatarBaseProps, useAvatarGroup } from "./avatar-group-context";
@@ -45,7 +46,7 @@ export const AvatarRoot = (props: PrimitiveProps<"div", AvatarRootProps>) => {
4546
"size-(--sv) relative inline-flex shrink-0 select-none items-center justify-center",
4647
radiusClass,
4748
group && "border-bg-normal not-first:ms-(--pv) border-2",
48-
!className?.split(" ").some((cls) => cls.startsWith("bg-")) && "bg-bg-subtlest",
49+
!hasBackgroundClass(className) && "bg-bg-subtlest",
4950
className,
5051
)}
5152
{...rest}

packages/react-ui/src/components/dialog/dialog-content.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { PrimitiveProps } from "../../primitives";
33
import { usePopperAria } from "../../primitives/popper/popper-aria-context";
44
import { PopperFloating } from "../../primitives/popper/popper-floating";
55
import { usePopperTransition } from "../../primitives/popper/popper-transtion-context";
6+
import { hasBackgroundClass } from "../../shared/utils";
67
import { tx } from "../../utils";
78
import { useDialog } from "./dialog-context";
89

@@ -34,7 +35,7 @@ export const DialogContent = (props: PrimitiveProps<"div">) => {
3435
"relative mx-auto shadow-md transition-opacity",
3536
status == "open" ? "opacity-100" : "opacity-0",
3637
scrollBehavior == "inside" && "max-h-[calc(100%-10rem)]",
37-
!className?.split(" ").some((cls) => cls.startsWith("bg-")) && "bg-bg-normal",
38+
!hasBackgroundClass(className) && "bg-bg-normal",
3839
className,
3940
)}
4041
aria-labelledby={labelId}

packages/react-ui/src/components/drawer/drawer-content.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { PrimitiveProps } from "../../primitives";
33
import { usePopperAria } from "../../primitives/popper/popper-aria-context";
44
import { PopperFloating } from "../../primitives/popper/popper-floating";
55
import { usePopperTransition } from "../../primitives/popper/popper-transtion-context";
6+
import { hasBackgroundClass } from "../../shared/utils";
67
import { tx } from "../../utils";
78
import { useDialog } from "../dialog/dialog-context";
89
import { useDrawer } from "./drawer-context";
@@ -52,7 +53,7 @@ export const DrawerContent = (props: PrimitiveProps<"div">) => {
5253
className={tx(
5354
"fixed flex flex-col shadow-md transition-[opacity,translate]",
5455
drawerStyle.base,
55-
!className?.split(" ").some((cls) => cls.startsWith("bg-")) && "bg-bg-normal",
56+
!hasBackgroundClass(className) && "bg-bg-normal",
5657
status == "open" ? ["opacity-100", drawerStyle.open] : ["opacity-0", drawerStyle.close],
5758
className,
5859
)}

packages/react-ui/src/components/input/input.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ export type InputProps = Partial<InputGroupContextValue> & {
3737
required?: boolean;
3838

3939
/**
40-
* 是否无效
40+
* 是否只读
4141
* @default false
4242
*/
43-
invalid?: boolean;
43+
readOnly?: boolean;
4444

4545
/**
46-
* 是否只读
46+
* 是否无效
4747
* @default false
4848
*/
49-
readOnly?: boolean;
49+
invalid?: boolean;
5050

5151
/**
5252
* 是否全宽度

packages/react-ui/src/components/popover/popover-content.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { usePopperAria } from "../../primitives/popper/popper-aria-context";
44
import { PopperFloating } from "../../primitives/popper/popper-floating";
55
import { PopperPositioner } from "../../primitives/popper/popper-positioner";
66
import { usePopperTransition } from "../../primitives/popper/popper-transtion-context";
7+
import { hasBackgroundClass } from "../../shared/utils";
78
import { tx } from "../../utils";
89
import { Portal } from "../portal/portal";
910
import { usePopoverRoot } from "./popover-root-context";
@@ -29,7 +30,7 @@ export const PopoverContent = (props: PrimitiveProps<"div">) => {
2930
className={tx(
3031
"border-bd-normal relative border shadow-md transition-opacity",
3132
status == "open" ? "opacity-100" : "opacity-0",
32-
!className?.split(" ").some((cls) => cls.startsWith("bg-")) && "bg-bg-normal",
33+
!hasBackgroundClass(className) && "bg-bg-normal",
3334
className,
3435
)}
3536
aria-labelledby={labelId}

packages/react-ui/src/components/radio/radio-group-context.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export type RadioGroupBaseProps = RadioBaseProps & {
4646
invalid?: boolean;
4747

4848
/**
49-
*
49+
* 可控值
5050
*/
5151
value?: string | number;
5252
};

packages/react-ui/src/primitives/popper/popper-backdrop.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { CSSProperties } from "react";
2+
import { hasBackgroundClass } from "../../shared/utils";
23
import { tx } from "../../utils";
34
import type { PrimitiveProps } from "../index";
45
import { usePopperTransition } from "./popper-transtion-context";
@@ -19,7 +20,7 @@ export const PopperBackdrop = (props: PrimitiveProps<"div">) => {
1920
"fixed inset-0 z-50 overflow-auto",
2021
"duration-(--dv) transition-opacity",
2122
status == "open" ? "opacity-30" : "opacity-0",
22-
!className?.split(" ").some((cls) => cls.startsWith("bg-")) && "bg-black",
23+
!hasBackgroundClass(className) && "bg-black",
2324
className,
2425
)}
2526
{...rest}

packages/react-ui/src/shared/types.ts

+47
Original file line numberDiff line numberDiff line change
@@ -1 +1,48 @@
11
export type Orientation = "horizontal" | "vertical";
2+
3+
export type SingleValueProps = {
4+
/**
5+
* 是否多选
6+
* @default false
7+
*/
8+
multiple?: false;
9+
10+
/**
11+
* 受控值
12+
*/
13+
value?: string | number | null;
14+
15+
/**
16+
* 默认值
17+
* @default null
18+
*/
19+
defaultValue?: string | number | null;
20+
21+
/**
22+
* onChange 回调
23+
*/
24+
onChange?: (value: string | number | null) => void;
25+
};
26+
27+
export type MultipleValueProps = {
28+
/**
29+
* 是否多选
30+
*/
31+
multiple?: true;
32+
33+
/**
34+
* 受控值
35+
*/
36+
value?: (string | number)[];
37+
38+
/**
39+
* 默认值
40+
* default []
41+
*/
42+
defaultValue?: (string | number)[];
43+
44+
/**
45+
* onChange 回调
46+
*/
47+
onChange?: (value: (string | number)[]) => void;
48+
};

packages/react-ui/src/shared/utils.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const hasBackgroundClass = (className?: string) => {
2+
if (!className) {
3+
return false;
4+
}
5+
6+
return className.includes("bg-") && className.split(" ").some((cls) => cls.startsWith("bg-"));
7+
};

0 commit comments

Comments
 (0)