aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/pages/IconsPage.tsx
blob: 6b836cac54caab8a2293b922879464156034f470 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import { useMemo, useState } from 'react'
import { Button } from '@astryxdesign/core/Button'
import { Center } from '@astryxdesign/core/Center'
import { ClickableCard } from '@astryxdesign/core/ClickableCard'
import { Divider } from '@astryxdesign/core/Divider'
import { Grid } from '@astryxdesign/core/Grid'
import { Heading } from '@astryxdesign/core/Heading'
import { HStack } from '@astryxdesign/core/HStack'
import { Icon } from '@astryxdesign/core/Icon'
import type { IconName } from '@astryxdesign/core/Icon'
import { Text } from '@astryxdesign/core/Text'
import { TextInput } from '@astryxdesign/core/TextInput'
import { useToast } from '@astryxdesign/core/Toast'
import { ToggleButton, ToggleButtonGroup } from '@astryxdesign/core/ToggleButton'
import { VStack } from '@astryxdesign/core/VStack'
import {
  ChevronLeft,
  ChevronRight,
  icons,
  Search,
  type LucideIcon,
} from 'lucide-react'
import { Page, PageHeader } from '../components/Page'

type IconSet = 'semantic' | 'lucide'

interface SemanticIcon {
  name: IconName
  label: string
}

const SEMANTIC_ICONS: SemanticIcon[] = [
  { name: 'close', label: '关闭' },
  { name: 'chevronDown', label: '向下展开' },
  { name: 'chevronLeft', label: '向左' },
  { name: 'chevronRight', label: '向右' },
  { name: 'chevronsLeft', label: '跳到开头' },
  { name: 'chevronsRight', label: '跳到末尾' },
  { name: 'check', label: '确认' },
  { name: 'success', label: '成功' },
  { name: 'error', label: '错误' },
  { name: 'warning', label: '警告' },
  { name: 'info', label: '信息' },
  { name: 'calendar', label: '日历' },
  { name: 'clock', label: '时间' },
  { name: 'externalLink', label: '外部链接' },
  { name: 'menu', label: '菜单' },
  { name: 'moreHorizontal', label: '更多' },
  { name: 'search', label: '搜索' },
  { name: 'upload', label: '上传' },
  { name: 'arrowUp', label: '向上' },
  { name: 'arrowDown', label: '向下' },
  { name: 'arrowsUpDown', label: '排序' },
  { name: 'funnel', label: '筛选' },
  { name: 'eyeSlash', label: '隐藏' },
  { name: 'viewColumns', label: '列设置' },
  { name: 'copy', label: '复制' },
  { name: 'checkDouble', label: '已复制' },
  { name: 'wrench', label: '设置' },
  { name: 'stop', label: '停止' },
  { name: 'microphone', label: '麦克风' },
]

interface LucideEntry {
  name: string
  Icon: LucideIcon
}

const LUCIDE_ICONS: LucideEntry[] = Object.entries(icons)
  .map(([name, Icon]) => ({ name, Icon }))
  .sort((a, b) => a.name.localeCompare(b.name))

const LUCIDE_PAGE_SIZE = 120

export function IconsPage() {
  const [activeSet, setActiveSet] = useState<IconSet>('semantic')
  const [query, setQuery] = useState('')
  const [page, setPage] = useState(1)
  const showToast = useToast()

  const changeSet = (value: string | null) => {
    setActiveSet(value === 'lucide' ? 'lucide' : 'semantic')
    setPage(1)
  }

  const changeQuery = (value: string) => {
    setQuery(value)
    setPage(1)
  }

  const copyUsage = async (code: string, name: string) => {
    try {
      if (!navigator.clipboard) throw new Error('Clipboard unavailable')
      await navigator.clipboard.writeText(code)
      showToast({ body: `已复制 ${name} 的用法`, type: 'info' })
    } catch {
      showToast({ body: '复制失败,请检查浏览器权限', type: 'error' })
    }
  }

  const normalizedQuery = query.trim().toLowerCase()

  const filteredSemantic = useMemo(
    () =>
      SEMANTIC_ICONS.filter(
        (icon) =>
          normalizedQuery.length === 0 ||
          icon.name.toLowerCase().includes(normalizedQuery) ||
          icon.label.includes(query.trim()),
      ),
    [normalizedQuery, query],
  )

  const filteredLucide = useMemo(
    () =>
      normalizedQuery.length === 0
        ? LUCIDE_ICONS
        : LUCIDE_ICONS.filter((icon) =>
            icon.name.toLowerCase().includes(normalizedQuery),
          ),
    [normalizedQuery],
  )

  const totalPages = Math.max(1, Math.ceil(filteredLucide.length / LUCIDE_PAGE_SIZE))
  const currentPage = Math.min(page, totalPages)
  const pageStart = (currentPage - 1) * LUCIDE_PAGE_SIZE
  const pageItems = filteredLucide.slice(
    pageStart,
    pageStart + LUCIDE_PAGE_SIZE,
  )

  const isSemantic = activeSet === 'semantic'
  const visibleCount = isSemantic ? filteredSemantic.length : filteredLucide.length

  return (
    <Page>
      <VStack gap={8}>
        <PageHeader
          eyebrow="图标资源"
          title="图标"
          description={`浏览 ${SEMANTIC_ICONS.length} 个 Astryx 语义图标和 ${LUCIDE_ICONS.length} 个 Lucide 图标。点击任意图标即可复制它的用法。`}
        />

        <Divider />

        <VStack gap={4}>
          <HStack gap={4} wrap="wrap" vAlign="end">
            <TextInput
              label="搜索图标"
              isLabelHidden
              placeholder="搜索图标名称"
              value={query}
              onChange={changeQuery}
              startIcon={Search}
              size="lg"
              width={360}
            />
            <ToggleButtonGroup
              label="选择图标集"
              value={activeSet}
              onChange={changeSet}
              size="sm">
              <ToggleButton
                label={`语义图标 ${SEMANTIC_ICONS.length}`}
                value="semantic"
              />
              <ToggleButton
                label={`Lucide ${LUCIDE_ICONS.length}`}
                value="lucide"
              />
            </ToggleButtonGroup>
          </HStack>
          <Text type="supporting" size="sm">
            {isSemantic
              ? '语义图标通过名称使用,会自动适配当前主题。'
              : 'Lucide 图标以组件形式传入 Icon,可与语义图标混用。'}
          </Text>
        </VStack>

        {visibleCount === 0 ? (
          <Center minHeight={220}>
            <VStack gap={2} hAlign="center">
              <Icon icon={Search} size="lg" color="secondary" />
              <Text type="label">没有找到匹配的图标。</Text>
              <Text type="supporting" color="secondary">
                试试更短的关键词。
              </Text>
            </VStack>
          </Center>
        ) : isSemantic ? (
          <>
            <Text type="supporting" size="sm">
              共 {filteredSemantic.length} 个语义图标
            </Text>
            <Grid columns={{ minWidth: 160, max: 4 }} gap={3}>
              {filteredSemantic.map((semanticIcon) => (
                <ClickableCard
                  key={semanticIcon.name}
                  padding={4}
                  onClick={() =>
                    void copyUsage(
                      `<Icon icon="${semanticIcon.name}" size="lg" />`,
                      semanticIcon.name,
                    )
                  }
                  label={`复制 ${semanticIcon.name} 图标用法`}>
                  <VStack gap={2} hAlign="center">
                    <Icon
                      icon={semanticIcon.name}
                      size="lg"
                      color="accent"
                    />
                    <Text
                      type="code"
                      size="sm"
                      display="block"
                      justify="center"
                      maxLines={1}>
                      {semanticIcon.name}
                    </Text>
                    <Text
                      type="supporting"
                      size="sm"
                      display="block"
                      justify="center">
                      {semanticIcon.label}
                    </Text>
                  </VStack>
                </ClickableCard>
              ))}
            </Grid>
          </>
        ) : (
          <>
            <HStack gap={2} vAlign="center" hAlign="between" wrap="wrap">
              <Text type="supporting" size="sm">
                显示 {pageStart + 1}–{Math.min(pageStart + LUCIDE_PAGE_SIZE, filteredLucide.length)} 个,共 {filteredLucide.length} 个图标
              </Text>
              <HStack gap={2} vAlign="center">
                <Button
                  label="上一页"
                  variant="secondary"
                  size="sm"
                  isDisabled={currentPage === 1}
                  onClick={() => setPage((value) => Math.max(1, value - 1))}
                  icon={<Icon icon={ChevronLeft} size="sm" />}
                />
                <Text type="supporting" size="sm">
                  第 {currentPage} / {totalPages} 页
                </Text>
                <Button
                  label="下一页"
                  variant="secondary"
                  size="sm"
                  isDisabled={currentPage === totalPages}
                  onClick={() =>
                    setPage((value) => Math.min(totalPages, value + 1))
                  }
                  endContent={<Icon icon={ChevronRight} size="sm" />}
                />
              </HStack>
            </HStack>

            <Grid columns={{ minWidth: 160, max: 5 }} gap={3}>
              {pageItems.map(({ name, Icon: LucideComponent }) => (
                <ClickableCard
                  key={name}
                  padding={3}
                  onClick={() =>
                    void copyUsage(
                      `import { ${name} } from 'lucide-react'\n\n<Icon icon={${name}} size="lg" />`,
                      name,
                    )
                  }
                  label={`复制 ${name} 图标用法`}>
                  <VStack gap={2} hAlign="center">
                    <Icon icon={LucideComponent} size="lg" color="primary" />
                    <Text
                      type="supporting"
                      size="xsm"
                      display="block"
                      justify="center"
                      maxLines={2}
                      wordBreak="break-all">
                      {name}
                    </Text>
                  </VStack>
                </ClickableCard>
              ))}
            </Grid>

            <HStack gap={2} vAlign="center" hAlign="between" wrap="wrap">
              <Text type="supporting" size="sm">
                点击图标复制 import 与 Icon 用法
              </Text>
              <HStack gap={2} vAlign="center">
                <Button
                  label="上一页"
                  variant="secondary"
                  size="sm"
                  isDisabled={currentPage === 1}
                  onClick={() => setPage((value) => Math.max(1, value - 1))}
                  icon={<Icon icon={ChevronLeft} size="sm" />}
                />
                <Text type="supporting" size="sm">
                  第 {currentPage} / {totalPages} 页
                </Text>
                <Button
                  label="下一页"
                  variant="secondary"
                  size="sm"
                  isDisabled={currentPage === totalPages}
                  onClick={() =>
                    setPage((value) => Math.min(totalPages, value + 1))
                  }
                  endContent={<Icon icon={ChevronRight} size="sm" />}
                />
              </HStack>
            </HStack>
          </>
        )}

        <Divider />

        <VStack gap={1}>
          <Heading level={2}>使用方式</Heading>
          <Text type="body" color="secondary" as="p">
            语义图标可以直接传入名称;Lucide 图标需要先导入组件,再交给 Icon 渲染。
          </Text>
        </VStack>
      </VStack>
    </Page>
  )
}