@lark-apaas/coding-template-react-standard-webapp 0.1.0-alpha.20260827082008
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +13 -0
- package/README.md +25 -0
- package/hooks/after-install.mjs +3 -0
- package/hooks/after-render.mjs +3 -0
- package/hooks/before-render.mjs +3 -0
- package/package.json +35 -0
- package/template/README.md +105 -0
- package/template/_gitignore +64 -0
- package/template/_npmrc +1 -0
- package/template/components.json +21 -0
- package/template/eslint.config.mjs +27 -0
- package/template/index.html +13 -0
- package/template/miaoda.json +11 -0
- package/template/package-lock.json +7685 -0
- package/template/package.json +89 -0
- package/template/public/favicon.svg +1 -0
- package/template/public/icons.svg +24 -0
- package/template/src/app.tsx +15 -0
- package/template/src/components/ErrorFallback.tsx +19 -0
- package/template/src/components/Layout.tsx +7 -0
- package/template/src/components/ui/accordion.tsx +66 -0
- package/template/src/components/ui/alert-dialog.tsx +157 -0
- package/template/src/components/ui/alert.tsx +71 -0
- package/template/src/components/ui/aspect-ratio.tsx +11 -0
- package/template/src/components/ui/avatar.tsx +53 -0
- package/template/src/components/ui/badge.tsx +42 -0
- package/template/src/components/ui/breadcrumb.tsx +109 -0
- package/template/src/components/ui/button-group.tsx +83 -0
- package/template/src/components/ui/button.tsx +69 -0
- package/template/src/components/ui/calendar.tsx +213 -0
- package/template/src/components/ui/card.tsx +82 -0
- package/template/src/components/ui/carousel.tsx +241 -0
- package/template/src/components/ui/chart.tsx +357 -0
- package/template/src/components/ui/checkbox.tsx +32 -0
- package/template/src/components/ui/collapsible.tsx +33 -0
- package/template/src/components/ui/command.tsx +208 -0
- package/template/src/components/ui/context-menu.tsx +324 -0
- package/template/src/components/ui/dialog.tsx +143 -0
- package/template/src/components/ui/drawer.tsx +135 -0
- package/template/src/components/ui/dropdown-menu.tsx +329 -0
- package/template/src/components/ui/empty.tsx +104 -0
- package/template/src/components/ui/field.tsx +248 -0
- package/template/src/components/ui/form.tsx +167 -0
- package/template/src/components/ui/hover-card.tsx +44 -0
- package/template/src/components/ui/image.tsx +183 -0
- package/template/src/components/ui/input-group.tsx +166 -0
- package/template/src/components/ui/input-otp.tsx +77 -0
- package/template/src/components/ui/input.tsx +21 -0
- package/template/src/components/ui/item.tsx +193 -0
- package/template/src/components/ui/kbd.tsx +28 -0
- package/template/src/components/ui/label.tsx +24 -0
- package/template/src/components/ui/menubar.tsx +348 -0
- package/template/src/components/ui/native-select.tsx +48 -0
- package/template/src/components/ui/navigation-menu.tsx +168 -0
- package/template/src/components/ui/pagination.tsx +127 -0
- package/template/src/components/ui/popover.tsx +48 -0
- package/template/src/components/ui/progress.tsx +31 -0
- package/template/src/components/ui/radio-group.tsx +45 -0
- package/template/src/components/ui/resizable.tsx +56 -0
- package/template/src/components/ui/scroll-area.tsx +58 -0
- package/template/src/components/ui/select.tsx +243 -0
- package/template/src/components/ui/separator.tsx +28 -0
- package/template/src/components/ui/sheet.tsx +139 -0
- package/template/src/components/ui/sidebar.tsx +727 -0
- package/template/src/components/ui/skeleton.tsx +13 -0
- package/template/src/components/ui/slider.tsx +87 -0
- package/template/src/components/ui/sonner.tsx +67 -0
- package/template/src/components/ui/spinner.tsx +16 -0
- package/template/src/components/ui/switch.tsx +31 -0
- package/template/src/components/ui/table.tsx +116 -0
- package/template/src/components/ui/tabs.tsx +66 -0
- package/template/src/components/ui/textarea.tsx +18 -0
- package/template/src/components/ui/toggle-group.tsx +83 -0
- package/template/src/components/ui/toggle.tsx +47 -0
- package/template/src/components/ui/tooltip.tsx +61 -0
- package/template/src/hooks/use-mobile.ts +20 -0
- package/template/src/index.css +4 -0
- package/template/src/index.tsx +17 -0
- package/template/src/lib/utils.ts +6 -0
- package/template/src/pages/ExamplePage/ExamplePage.tsx +37 -0
- package/template/src/pages/HomePage/HomePage.tsx +10 -0
- package/template/src/pages/NotFoundPage/NotFoundPage.tsx +11 -0
- package/template/src/tailwind-theme.css +258 -0
- package/template/src/typography.css +41 -0
- package/template/src/vite-env.d.ts +5 -0
- package/template/tsconfig.app.json +29 -0
- package/template/tsconfig.json +10 -0
- package/template/tsconfig.node.json +26 -0
- package/template/vite.config.ts +97 -0
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { useMemo } from "react"
|
|
4
|
+
import { cva, type VariantProps } from "class-variance-authority"
|
|
5
|
+
|
|
6
|
+
import { cn } from "@/lib/utils"
|
|
7
|
+
import { Label } from "@/components/ui/label"
|
|
8
|
+
import { Separator } from "@/components/ui/separator"
|
|
9
|
+
|
|
10
|
+
function FieldSet({ className, ...props }: React.ComponentProps<"fieldset">) {
|
|
11
|
+
return (
|
|
12
|
+
<fieldset
|
|
13
|
+
data-slot="field-set"
|
|
14
|
+
className={cn(
|
|
15
|
+
"flex flex-col gap-6",
|
|
16
|
+
"has-[>[data-slot=checkbox-group]]:gap-3 has-[>[data-slot=radio-group]]:gap-3",
|
|
17
|
+
className
|
|
18
|
+
)}
|
|
19
|
+
{...props}
|
|
20
|
+
/>
|
|
21
|
+
)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function FieldLegend({
|
|
25
|
+
className,
|
|
26
|
+
variant = "legend",
|
|
27
|
+
...props
|
|
28
|
+
}: React.ComponentProps<"legend"> & { variant?: "legend" | "label" }) {
|
|
29
|
+
return (
|
|
30
|
+
<legend
|
|
31
|
+
data-slot="field-legend"
|
|
32
|
+
data-variant={variant}
|
|
33
|
+
className={cn(
|
|
34
|
+
"mb-3 font-normal",
|
|
35
|
+
"data-[variant=legend]:text-base",
|
|
36
|
+
"data-[variant=label]:text-sm",
|
|
37
|
+
className
|
|
38
|
+
)}
|
|
39
|
+
{...props}
|
|
40
|
+
/>
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function FieldGroup({ className, ...props }: React.ComponentProps<"div">) {
|
|
45
|
+
return (
|
|
46
|
+
<div
|
|
47
|
+
data-slot="field-group"
|
|
48
|
+
className={cn(
|
|
49
|
+
"group/field-group @container/field-group flex w-full flex-col gap-7 data-[slot=checkbox-group]:gap-3 [&>[data-slot=field-group]]:gap-4",
|
|
50
|
+
className
|
|
51
|
+
)}
|
|
52
|
+
{...props}
|
|
53
|
+
/>
|
|
54
|
+
)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const fieldVariants = cva(
|
|
58
|
+
"group/field flex w-full gap-3 data-[invalid=true]:text-destructive",
|
|
59
|
+
{
|
|
60
|
+
variants: {
|
|
61
|
+
orientation: {
|
|
62
|
+
vertical: ["flex-col [&>*]:w-full [&>.sr-only]:w-auto"],
|
|
63
|
+
horizontal: [
|
|
64
|
+
"flex-row items-center",
|
|
65
|
+
"[&>[data-slot=field-label]]:flex-auto",
|
|
66
|
+
"has-[>[data-slot=field-content]]:items-start has-[>[data-slot=field-content]]:[&>[role=checkbox],[role=radio]]:mt-px",
|
|
67
|
+
],
|
|
68
|
+
responsive: [
|
|
69
|
+
"flex-col [&>*]:w-full [&>.sr-only]:w-auto @md/field-group:flex-row @md/field-group:items-center @md/field-group:[&>*]:w-auto",
|
|
70
|
+
"@md/field-group:[&>[data-slot=field-label]]:flex-auto",
|
|
71
|
+
"@md/field-group:has-[>[data-slot=field-content]]:items-start @md/field-group:has-[>[data-slot=field-content]]:[&>[role=checkbox],[role=radio]]:mt-px",
|
|
72
|
+
],
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
defaultVariants: {
|
|
76
|
+
orientation: "vertical",
|
|
77
|
+
},
|
|
78
|
+
}
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
function Field({
|
|
82
|
+
className,
|
|
83
|
+
orientation = "vertical",
|
|
84
|
+
...props
|
|
85
|
+
}: React.ComponentProps<"div"> & VariantProps<typeof fieldVariants>) {
|
|
86
|
+
return (
|
|
87
|
+
<div
|
|
88
|
+
role="group"
|
|
89
|
+
data-slot="field"
|
|
90
|
+
data-orientation={orientation}
|
|
91
|
+
className={cn(fieldVariants({ orientation }), className)}
|
|
92
|
+
{...props}
|
|
93
|
+
/>
|
|
94
|
+
)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function FieldContent({ className, ...props }: React.ComponentProps<"div">) {
|
|
98
|
+
return (
|
|
99
|
+
<div
|
|
100
|
+
data-slot="field-content"
|
|
101
|
+
className={cn(
|
|
102
|
+
"group/field-content flex flex-1 flex-col gap-1.5 leading-snug",
|
|
103
|
+
className
|
|
104
|
+
)}
|
|
105
|
+
{...props}
|
|
106
|
+
/>
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function FieldLabel({
|
|
111
|
+
className,
|
|
112
|
+
...props
|
|
113
|
+
}: React.ComponentProps<typeof Label>) {
|
|
114
|
+
return (
|
|
115
|
+
<Label
|
|
116
|
+
data-slot="field-label"
|
|
117
|
+
className={cn(
|
|
118
|
+
"group/field-label peer/field-label flex w-fit gap-2 leading-snug group-data-[disabled=true]/field:opacity-50",
|
|
119
|
+
"has-[>[data-slot=field]]:w-full has-[>[data-slot=field]]:flex-col has-[>[data-slot=field]]:rounded-md has-[>[data-slot=field]]:border [&>*]:data-[slot=field]:p-4",
|
|
120
|
+
"has-data-[state=checked]:bg-primary/5 has-data-[state=checked]:border-primary dark:has-data-[state=checked]:bg-primary/10",
|
|
121
|
+
className
|
|
122
|
+
)}
|
|
123
|
+
{...props}
|
|
124
|
+
/>
|
|
125
|
+
)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function FieldTitle({ className, ...props }: React.ComponentProps<"div">) {
|
|
129
|
+
return (
|
|
130
|
+
<div
|
|
131
|
+
data-slot="field-label"
|
|
132
|
+
className={cn(
|
|
133
|
+
"flex w-fit items-center gap-2 text-sm leading-snug font-normal group-data-[disabled=true]/field:opacity-50",
|
|
134
|
+
className
|
|
135
|
+
)}
|
|
136
|
+
{...props}
|
|
137
|
+
/>
|
|
138
|
+
)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function FieldDescription({ className, ...props }: React.ComponentProps<"p">) {
|
|
142
|
+
return (
|
|
143
|
+
<p
|
|
144
|
+
data-slot="field-description"
|
|
145
|
+
className={cn(
|
|
146
|
+
"text-muted-foreground text-sm leading-normal font-normal group-has-[[data-orientation=horizontal]]/field:text-balance",
|
|
147
|
+
"last:mt-0 nth-last-2:-mt-1 [[data-variant=legend]+&]:-mt-1.5",
|
|
148
|
+
"[&>a:hover]:text-primary [&>a]:underline [&>a]:underline-offset-4",
|
|
149
|
+
className
|
|
150
|
+
)}
|
|
151
|
+
{...props}
|
|
152
|
+
/>
|
|
153
|
+
)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function FieldSeparator({
|
|
157
|
+
children,
|
|
158
|
+
className,
|
|
159
|
+
...props
|
|
160
|
+
}: React.ComponentProps<"div"> & {
|
|
161
|
+
children?: React.ReactNode
|
|
162
|
+
}) {
|
|
163
|
+
return (
|
|
164
|
+
<div
|
|
165
|
+
data-slot="field-separator"
|
|
166
|
+
data-content={!!children}
|
|
167
|
+
className={cn(
|
|
168
|
+
"relative -my-2 h-5 text-sm group-data-[variant=outline]/field-group:-mb-2",
|
|
169
|
+
className
|
|
170
|
+
)}
|
|
171
|
+
{...props}
|
|
172
|
+
>
|
|
173
|
+
<Separator className="absolute inset-0 top-1/2" />
|
|
174
|
+
{children && (
|
|
175
|
+
<span
|
|
176
|
+
className="bg-background text-muted-foreground relative mx-auto block w-fit px-2"
|
|
177
|
+
data-slot="field-separator-content"
|
|
178
|
+
>
|
|
179
|
+
{children}
|
|
180
|
+
</span>
|
|
181
|
+
)}
|
|
182
|
+
</div>
|
|
183
|
+
)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function FieldError({
|
|
187
|
+
className,
|
|
188
|
+
children,
|
|
189
|
+
errors,
|
|
190
|
+
...props
|
|
191
|
+
}: React.ComponentProps<"div"> & {
|
|
192
|
+
errors?: Array<{ message?: string } | undefined>
|
|
193
|
+
}) {
|
|
194
|
+
const content = useMemo(() => {
|
|
195
|
+
if (children) {
|
|
196
|
+
return children
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (!errors?.length) {
|
|
200
|
+
return null
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const uniqueErrors = [
|
|
204
|
+
...new Map(errors.map((error) => [error?.message, error])).values(),
|
|
205
|
+
]
|
|
206
|
+
|
|
207
|
+
if (uniqueErrors?.length == 1) {
|
|
208
|
+
return uniqueErrors[0]?.message
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return (
|
|
212
|
+
<ul className="ml-4 flex list-disc flex-col gap-1">
|
|
213
|
+
{uniqueErrors.map(
|
|
214
|
+
(error, index) =>
|
|
215
|
+
error?.message && <li key={index}>{error.message}</li>
|
|
216
|
+
)}
|
|
217
|
+
</ul>
|
|
218
|
+
)
|
|
219
|
+
}, [children, errors])
|
|
220
|
+
|
|
221
|
+
if (!content) {
|
|
222
|
+
return null
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return (
|
|
226
|
+
<div
|
|
227
|
+
role="alert"
|
|
228
|
+
data-slot="field-error"
|
|
229
|
+
className={cn("text-destructive text-sm font-normal", className)}
|
|
230
|
+
{...props}
|
|
231
|
+
>
|
|
232
|
+
{content}
|
|
233
|
+
</div>
|
|
234
|
+
)
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export {
|
|
238
|
+
Field,
|
|
239
|
+
FieldLabel,
|
|
240
|
+
FieldDescription,
|
|
241
|
+
FieldError,
|
|
242
|
+
FieldGroup,
|
|
243
|
+
FieldLegend,
|
|
244
|
+
FieldSeparator,
|
|
245
|
+
FieldSet,
|
|
246
|
+
FieldContent,
|
|
247
|
+
FieldTitle,
|
|
248
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import * as LabelPrimitive from "@radix-ui/react-label"
|
|
5
|
+
import { Slot } from "@radix-ui/react-slot"
|
|
6
|
+
import {
|
|
7
|
+
Controller,
|
|
8
|
+
FormProvider,
|
|
9
|
+
useFormContext,
|
|
10
|
+
useFormState,
|
|
11
|
+
type ControllerProps,
|
|
12
|
+
type FieldPath,
|
|
13
|
+
type FieldValues,
|
|
14
|
+
} from "react-hook-form"
|
|
15
|
+
|
|
16
|
+
import { cn } from "@/lib/utils"
|
|
17
|
+
import { Label } from "@/components/ui/label"
|
|
18
|
+
|
|
19
|
+
const Form = FormProvider
|
|
20
|
+
|
|
21
|
+
type FormFieldContextValue<
|
|
22
|
+
TFieldValues extends FieldValues = FieldValues,
|
|
23
|
+
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
|
|
24
|
+
> = {
|
|
25
|
+
name: TName
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const FormFieldContext = React.createContext<FormFieldContextValue>(
|
|
29
|
+
{} as FormFieldContextValue
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
const FormField = <
|
|
33
|
+
TFieldValues extends FieldValues = FieldValues,
|
|
34
|
+
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
|
|
35
|
+
>({
|
|
36
|
+
...props
|
|
37
|
+
}: ControllerProps<TFieldValues, TName>) => {
|
|
38
|
+
return (
|
|
39
|
+
<FormFieldContext.Provider value={{ name: props.name }}>
|
|
40
|
+
<Controller {...props} />
|
|
41
|
+
</FormFieldContext.Provider>
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const useFormField = () => {
|
|
46
|
+
const fieldContext = React.useContext(FormFieldContext)
|
|
47
|
+
const itemContext = React.useContext(FormItemContext)
|
|
48
|
+
const { getFieldState } = useFormContext()
|
|
49
|
+
const formState = useFormState({ name: fieldContext.name })
|
|
50
|
+
const fieldState = getFieldState(fieldContext.name, formState)
|
|
51
|
+
|
|
52
|
+
if (!fieldContext) {
|
|
53
|
+
throw new Error("useFormField should be used within <FormField>")
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const { id } = itemContext
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
id,
|
|
60
|
+
name: fieldContext.name,
|
|
61
|
+
formItemId: `${id}-form-item`,
|
|
62
|
+
formDescriptionId: `${id}-form-item-description`,
|
|
63
|
+
formMessageId: `${id}-form-item-message`,
|
|
64
|
+
...fieldState,
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
type FormItemContextValue = {
|
|
69
|
+
id: string
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const FormItemContext = React.createContext<FormItemContextValue>(
|
|
73
|
+
{} as FormItemContextValue
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
function FormItem({ className, ...props }: React.ComponentProps<"div">) {
|
|
77
|
+
const id = React.useId()
|
|
78
|
+
|
|
79
|
+
return (
|
|
80
|
+
<FormItemContext.Provider value={{ id }}>
|
|
81
|
+
<div
|
|
82
|
+
data-slot="form-item"
|
|
83
|
+
className={cn("grid gap-2", className)}
|
|
84
|
+
{...props}
|
|
85
|
+
/>
|
|
86
|
+
</FormItemContext.Provider>
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function FormLabel({
|
|
91
|
+
className,
|
|
92
|
+
...props
|
|
93
|
+
}: React.ComponentProps<typeof LabelPrimitive.Root>) {
|
|
94
|
+
const { error, formItemId } = useFormField()
|
|
95
|
+
|
|
96
|
+
return (
|
|
97
|
+
<Label
|
|
98
|
+
data-slot="form-label"
|
|
99
|
+
data-error={!!error}
|
|
100
|
+
className={cn("data-[error=true]:text-destructive", className)}
|
|
101
|
+
htmlFor={formItemId}
|
|
102
|
+
{...props}
|
|
103
|
+
/>
|
|
104
|
+
)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function FormControl({ ...props }: React.ComponentProps<typeof Slot>) {
|
|
108
|
+
const { error, formItemId, formDescriptionId, formMessageId } = useFormField()
|
|
109
|
+
|
|
110
|
+
return (
|
|
111
|
+
<Slot
|
|
112
|
+
data-slot="form-control"
|
|
113
|
+
id={formItemId}
|
|
114
|
+
aria-describedby={
|
|
115
|
+
!error
|
|
116
|
+
? `${formDescriptionId}`
|
|
117
|
+
: `${formDescriptionId} ${formMessageId}`
|
|
118
|
+
}
|
|
119
|
+
aria-invalid={!!error}
|
|
120
|
+
{...props}
|
|
121
|
+
/>
|
|
122
|
+
)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function FormDescription({ className, ...props }: React.ComponentProps<"p">) {
|
|
126
|
+
const { formDescriptionId } = useFormField()
|
|
127
|
+
|
|
128
|
+
return (
|
|
129
|
+
<p
|
|
130
|
+
data-slot="form-description"
|
|
131
|
+
id={formDescriptionId}
|
|
132
|
+
className={cn("text-muted-foreground text-sm", className)}
|
|
133
|
+
{...props}
|
|
134
|
+
/>
|
|
135
|
+
)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function FormMessage({ className, ...props }: React.ComponentProps<"p">) {
|
|
139
|
+
const { error, formMessageId } = useFormField()
|
|
140
|
+
const body = error ? String(error?.message ?? "") : props.children
|
|
141
|
+
|
|
142
|
+
if (!body) {
|
|
143
|
+
return null
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return (
|
|
147
|
+
<p
|
|
148
|
+
data-slot="form-message"
|
|
149
|
+
id={formMessageId}
|
|
150
|
+
className={cn("text-destructive text-sm", className)}
|
|
151
|
+
{...props}
|
|
152
|
+
>
|
|
153
|
+
{body}
|
|
154
|
+
</p>
|
|
155
|
+
)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export {
|
|
159
|
+
useFormField,
|
|
160
|
+
Form,
|
|
161
|
+
FormItem,
|
|
162
|
+
FormLabel,
|
|
163
|
+
FormControl,
|
|
164
|
+
FormDescription,
|
|
165
|
+
FormMessage,
|
|
166
|
+
FormField,
|
|
167
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import * as HoverCardPrimitive from "@radix-ui/react-hover-card"
|
|
5
|
+
|
|
6
|
+
import { cn } from "@/lib/utils"
|
|
7
|
+
|
|
8
|
+
function HoverCard({
|
|
9
|
+
...props
|
|
10
|
+
}: React.ComponentProps<typeof HoverCardPrimitive.Root>) {
|
|
11
|
+
return <HoverCardPrimitive.Root data-slot="hover-card" {...props} />
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function HoverCardTrigger({
|
|
15
|
+
...props
|
|
16
|
+
}: React.ComponentProps<typeof HoverCardPrimitive.Trigger>) {
|
|
17
|
+
return (
|
|
18
|
+
<HoverCardPrimitive.Trigger data-slot="hover-card-trigger" {...props} />
|
|
19
|
+
)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function HoverCardContent({
|
|
23
|
+
className,
|
|
24
|
+
align = "center",
|
|
25
|
+
sideOffset = 4,
|
|
26
|
+
...props
|
|
27
|
+
}: React.ComponentProps<typeof HoverCardPrimitive.Content>) {
|
|
28
|
+
return (
|
|
29
|
+
<HoverCardPrimitive.Portal data-slot="hover-card-portal">
|
|
30
|
+
<HoverCardPrimitive.Content
|
|
31
|
+
data-slot="hover-card-content"
|
|
32
|
+
align={align}
|
|
33
|
+
sideOffset={sideOffset}
|
|
34
|
+
className={cn(
|
|
35
|
+
"bg-popover text-popover-foreground data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 z-50 w-64 origin-(--radix-hover-card-content-transform-origin) rounded-md border p-4 shadow-md outline-hidden",
|
|
36
|
+
className
|
|
37
|
+
)}
|
|
38
|
+
{...props}
|
|
39
|
+
/>
|
|
40
|
+
</HoverCardPrimitive.Portal>
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export { HoverCard, HoverCardTrigger, HoverCardContent }
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import { cn } from '@/lib/utils';
|
|
5
|
+
type ImageFormat = 'jpg' | 'png' | 'webp' | 'bmp' | 'gif' | 'tiff';
|
|
6
|
+
|
|
7
|
+
type NativeImgProps = React.ComponentPropsWithoutRef<'img'>;
|
|
8
|
+
|
|
9
|
+
export interface ImageProps extends NativeImgProps {
|
|
10
|
+
quality?: number;
|
|
11
|
+
format?: ImageFormat;
|
|
12
|
+
breakpoints?: Array<number>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const DEFAULT_QUALITY = 80;
|
|
16
|
+
const DEFAULT_RESOLUTIONS: number[] = [
|
|
17
|
+
16, 32, 48, 64, 96, 128, 256, 384, 640, 750, 828, 1080, 1200, 1920, 2048,
|
|
18
|
+
3840,
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
const SRC_ALLOWLIST = [
|
|
22
|
+
'/runtime/api/v1/storage/object/',
|
|
23
|
+
'/aily/api/v1/feisuda/attachments/',
|
|
24
|
+
'/aily/api/v1/files/static/',
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
function getClosestResolution(target: number): number {
|
|
28
|
+
return DEFAULT_RESOLUTIONS.reduce((prev, curr) => {
|
|
29
|
+
return Math.abs(curr - target) < Math.abs(prev - target) ? curr : prev;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function applyParamsToUrl(
|
|
34
|
+
src: string,
|
|
35
|
+
params: Record<string, string | number | undefined>,
|
|
36
|
+
): string {
|
|
37
|
+
const search = Object.entries(params)
|
|
38
|
+
.filter(([, v]) => v !== undefined && v !== null && v !== '')
|
|
39
|
+
.map(([k, v]) => {
|
|
40
|
+
return `${k},${v}`;
|
|
41
|
+
})
|
|
42
|
+
.join('/');
|
|
43
|
+
if (!search) return src;
|
|
44
|
+
|
|
45
|
+
const [pathAndQuery = '', hash] = src.split('#');
|
|
46
|
+
const [base, query] = pathAndQuery.split('?');
|
|
47
|
+
const urlParams = new URLSearchParams(query);
|
|
48
|
+
urlParams.set('x-tos-process', `image/${search}`);
|
|
49
|
+
|
|
50
|
+
return `${base}?${urlParams.toString()}${hash ? '#' + hash : ''}`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function isTargetSrc(originSrc: string) {
|
|
54
|
+
return SRC_ALLOWLIST.some((item) => originSrc.includes(item));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function supportWebp() {
|
|
58
|
+
try {
|
|
59
|
+
return (
|
|
60
|
+
document
|
|
61
|
+
.createElement('canvas')
|
|
62
|
+
.toDataURL('image/webp')
|
|
63
|
+
.indexOf('data:image/webp') === 0
|
|
64
|
+
);
|
|
65
|
+
} catch (err) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function buildSrcSet(
|
|
71
|
+
src: string,
|
|
72
|
+
widths: number[],
|
|
73
|
+
format: ImageFormat | undefined,
|
|
74
|
+
quality: number,
|
|
75
|
+
width?: number,
|
|
76
|
+
sizes?: string,
|
|
77
|
+
): string | undefined {
|
|
78
|
+
if (!widths || widths.length === 0 || (!width && !sizes)) return undefined;
|
|
79
|
+
const fmt = format;
|
|
80
|
+
if (width) {
|
|
81
|
+
return [1, 2]
|
|
82
|
+
.map((dpr) => {
|
|
83
|
+
const targetWidth = getClosestResolution(width * dpr);
|
|
84
|
+
return `${applyParamsToUrl(src, { resize: `w_${targetWidth}`, quality: `Q_${quality}`, format: fmt })} ${dpr}x`;
|
|
85
|
+
})
|
|
86
|
+
.join(', ');
|
|
87
|
+
}
|
|
88
|
+
return widths
|
|
89
|
+
.map(
|
|
90
|
+
(w) =>
|
|
91
|
+
`${applyParamsToUrl(src, { resize: `w_${w}`, quality: `Q_${quality}`, format: fmt })} ${w}w`,
|
|
92
|
+
)
|
|
93
|
+
.join(', ');
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export const Image = React.forwardRef<HTMLImageElement, ImageProps>(
|
|
97
|
+
(
|
|
98
|
+
{
|
|
99
|
+
src,
|
|
100
|
+
width,
|
|
101
|
+
height,
|
|
102
|
+
quality = DEFAULT_QUALITY,
|
|
103
|
+
format,
|
|
104
|
+
sizes,
|
|
105
|
+
srcSet: userSrcSet,
|
|
106
|
+
breakpoints = DEFAULT_RESOLUTIONS,
|
|
107
|
+
className,
|
|
108
|
+
loading = 'lazy',
|
|
109
|
+
decoding = 'async',
|
|
110
|
+
...rest
|
|
111
|
+
},
|
|
112
|
+
ref,
|
|
113
|
+
) => {
|
|
114
|
+
const defaultFormat = React.useMemo(
|
|
115
|
+
() => (supportWebp() ? 'webp' : undefined),
|
|
116
|
+
[],
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
// 当 src 不在白名单时,直接渲染原生 img,保留所有原生属性
|
|
120
|
+
if (typeof src !== 'string' || !isTargetSrc(src)) {
|
|
121
|
+
return (
|
|
122
|
+
<img
|
|
123
|
+
{...rest}
|
|
124
|
+
ref={ref}
|
|
125
|
+
src={src}
|
|
126
|
+
width={width}
|
|
127
|
+
height={height}
|
|
128
|
+
sizes={sizes}
|
|
129
|
+
srcSet={userSrcSet}
|
|
130
|
+
className={cn(
|
|
131
|
+
'bg-linear-to-b from-gray-50/20 to-gray-200/20',
|
|
132
|
+
className,
|
|
133
|
+
)}
|
|
134
|
+
loading={loading}
|
|
135
|
+
decoding={decoding}
|
|
136
|
+
/>
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// 只有当 width 是数字类型时才进行 srcSet 优化
|
|
141
|
+
const numericWidth = typeof width === 'number' ? width : undefined;
|
|
142
|
+
|
|
143
|
+
// 用户传入的 srcSet 优先,否则生成优化的 srcSet
|
|
144
|
+
const srcSet =
|
|
145
|
+
userSrcSet ??
|
|
146
|
+
buildSrcSet(
|
|
147
|
+
src,
|
|
148
|
+
breakpoints,
|
|
149
|
+
format ?? (defaultFormat as ImageFormat),
|
|
150
|
+
quality,
|
|
151
|
+
numericWidth,
|
|
152
|
+
sizes,
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
const baseSrc = applyParamsToUrl(src, {
|
|
156
|
+
resize: numericWidth ? `w_${numericWidth}` : undefined,
|
|
157
|
+
quality: `Q_${quality}`,
|
|
158
|
+
format: format ?? defaultFormat,
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
return (
|
|
162
|
+
<img
|
|
163
|
+
{...rest}
|
|
164
|
+
ref={ref}
|
|
165
|
+
src={baseSrc}
|
|
166
|
+
width={width}
|
|
167
|
+
height={height}
|
|
168
|
+
sizes={sizes}
|
|
169
|
+
srcSet={srcSet}
|
|
170
|
+
className={cn(
|
|
171
|
+
'bg-linear-to-b from-gray-50/20 to-gray-200/20',
|
|
172
|
+
className,
|
|
173
|
+
)}
|
|
174
|
+
loading={loading}
|
|
175
|
+
decoding={decoding}
|
|
176
|
+
/>
|
|
177
|
+
);
|
|
178
|
+
},
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
Image.displayName = 'Image';
|
|
182
|
+
|
|
183
|
+
export default Image;
|