gi-component 0.0.39 → 0.0.40

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gi-component",
3
3
  "type": "module",
4
- "version": "0.0.39",
4
+ "version": "0.0.40",
5
5
  "description": "Vue3中基于Element Plus二次封装基础组件库",
6
6
  "author": "lin",
7
7
  "license": "MIT",
@@ -0,0 +1,5 @@
1
+ import Tag from './src/tag.vue'
2
+
3
+ export type TagInstance = InstanceType<typeof Tag>
4
+ export * from './src/type'
5
+ export default Tag
@@ -0,0 +1,342 @@
1
+ <template>
2
+ <span :class="className" :style="tagStyle" @click="handleClick">
3
+ <span v-if="hasIcon" :class="b('tag__icon')">
4
+ <slot name="icon">
5
+ <ElIcon v-if="icon">
6
+ <component :is="icon" />
7
+ </ElIcon>
8
+ </slot>
9
+ </span>
10
+ <slot />
11
+ <span v-if="closable" class="gi-tag-close-btn" @click="handleClose">
12
+ <ElIcon class="close-icon">
13
+ <Close />
14
+ </ElIcon>
15
+ </span>
16
+ </span>
17
+ </template>
18
+
19
+ <script setup lang="ts">
20
+ import type { CSSProperties } from 'vue'
21
+ import type { TagProps, TagThemeColor } from './type'
22
+ import { Close } from '@element-plus/icons-vue'
23
+ import { ElIcon } from 'element-plus'
24
+ import { computed, useSlots } from 'vue'
25
+ import { useBemClass } from '../../../hooks'
26
+
27
+ const props = withDefaults(defineProps<TagProps>(), {
28
+ type: 'light',
29
+ color: 'primary',
30
+ size: 'default',
31
+ closable: false
32
+ })
33
+
34
+ const emit = defineEmits<{
35
+ click: []
36
+ close: []
37
+ }>()
38
+
39
+ defineSlots<{
40
+ default?: () => unknown
41
+ icon?: () => unknown
42
+ }>()
43
+
44
+ const slots = useSlots()
45
+ const hasIcon = computed(() => Boolean(slots.icon) || Boolean(props.icon))
46
+
47
+ const SEMANTIC_THEME_COLORS: readonly TagThemeColor[] = [
48
+ 'primary',
49
+ 'success',
50
+ 'warning',
51
+ 'danger',
52
+ 'info'
53
+ ]
54
+
55
+ function isSemanticThemeColor(value: string): value is TagThemeColor {
56
+ return (SEMANTIC_THEME_COLORS as readonly string[]).includes(value)
57
+ }
58
+
59
+ const { b } = useBemClass()
60
+
61
+ const BASE_COLORS = {
62
+ red: '#FF0000',
63
+ orangered: '#f77234',
64
+ orange: '#ff7d00',
65
+ gold: '#f7ba1e',
66
+ lime: '#9fdb1d',
67
+ green: '#00b42a',
68
+ cyan: '#14c9c9',
69
+ blue: '#3491fa',
70
+ purple: '#722ed1',
71
+ pink: '#f5319d',
72
+ gray: '#86909c'
73
+ } as const
74
+
75
+ type PresetColorKey = keyof typeof BASE_COLORS
76
+
77
+ function resolveColorToken(input: string): string {
78
+ return BASE_COLORS[input as PresetColorKey] || input
79
+ }
80
+
81
+ function hexToRgb(hex: string): { r: number, g: number, b: number } {
82
+ const body = hex.startsWith('#') ? hex.slice(1) : hex
83
+ const full = body.length === 3
84
+ ? body.split('').map((c) => c + c).join('')
85
+ : body
86
+ return {
87
+ r: Number.parseInt(full.slice(0, 2), 16),
88
+ g: Number.parseInt(full.slice(2, 4), 16),
89
+ b: Number.parseInt(full.slice(4, 6), 16)
90
+ }
91
+ }
92
+
93
+ function stylesForType(
94
+ type: NonNullable<TagProps['type']>,
95
+ color: string,
96
+ rgb: { r: number, g: number, b: number }
97
+ ): CSSProperties {
98
+ const { r, g, b } = rgb
99
+ const bgTint = `rgba(${r}, ${g}, ${b}, 0.1)`
100
+ const borderTint = `rgba(${r}, ${g}, ${b}, 0.2)`
101
+ /** 与主题色标签一致:悬停时白字 + 实心色底 */
102
+ const closeHoverVars = {
103
+ '--tag-close-hover-color': '#fff',
104
+ '--tag-close-hover-bg-color': color
105
+ } as CSSProperties
106
+
107
+ switch (type) {
108
+ case 'dark':
109
+ return {
110
+ 'color': '#fff',
111
+ 'backgroundColor': color,
112
+ '--tag-close-hover-color': color,
113
+ '--tag-close-hover-bg-color': 'rgba(255, 255, 255, 0.9)'
114
+ } as CSSProperties
115
+ case 'outline':
116
+ return {
117
+ color,
118
+ backgroundColor: 'transparent',
119
+ borderColor: color,
120
+ ...closeHoverVars
121
+ }
122
+ case 'light-outline':
123
+ return {
124
+ color,
125
+ backgroundColor: bgTint,
126
+ borderColor: borderTint,
127
+ ...closeHoverVars
128
+ }
129
+ case 'light':
130
+ default:
131
+ return {
132
+ color,
133
+ backgroundColor: bgTint,
134
+ ...closeHoverVars
135
+ }
136
+ }
137
+ }
138
+
139
+ const className = computed(() => {
140
+ const c = props.color
141
+ return [
142
+ b('tag'),
143
+ props.type && b(`tag__type--${props.type}`),
144
+ props.size && b(`tag__size--${props.size}`),
145
+ c && isSemanticThemeColor(c) && b(`tag__color--${c}`)
146
+ ].filter(Boolean).join(' ')
147
+ })
148
+
149
+ const tagStyle = computed((): CSSProperties => {
150
+ const raw = props.color
151
+ if (!raw || isSemanticThemeColor(raw))
152
+ return {}
153
+ const color = resolveColorToken(raw)
154
+ return stylesForType(props.type ?? 'light', color, hexToRgb(color))
155
+ })
156
+
157
+ function handleClick() {
158
+ emit('click')
159
+ }
160
+
161
+ function handleClose(event: MouseEvent) {
162
+ event.stopPropagation()
163
+ emit('close')
164
+ }
165
+ </script>
166
+
167
+ <style scoped lang="scss">
168
+ @use '../../../styles/var.scss' as a;
169
+
170
+ $theme-colors: primary, success, warning, danger, info;
171
+
172
+ $tag-size-small-height: 20px;
173
+ $tag-size-default-height: 22px;
174
+ $tag-size-large-height: 24px;
175
+
176
+ $tag-size-small-padding: 0 6px;
177
+ $tag-size-default-padding: 0 8px;
178
+ $tag-size-large-padding: 0 10px;
179
+
180
+ .#{a.$prefix}-tag {
181
+ box-sizing: border-box;
182
+ display: inline-flex;
183
+ align-items: center;
184
+ justify-content: center;
185
+ font-size: 12px;
186
+ line-height: 1;
187
+ white-space: nowrap;
188
+ cursor: pointer;
189
+ border-radius: 3px;
190
+ }
191
+
192
+ .#{a.$prefix}-tag__icon {
193
+ display: inline-flex;
194
+ flex-shrink: 0;
195
+ align-items: center;
196
+ margin-right: 4px;
197
+ line-height: 0;
198
+ color: inherit;
199
+
200
+ :deep(.el-icon),
201
+ :deep(svg) {
202
+ width: 11px;
203
+ height: 11px;
204
+ }
205
+ }
206
+
207
+ .#{a.$prefix}-tag-close-btn {
208
+ position: relative;
209
+ box-sizing: border-box;
210
+ display: flex;
211
+ align-items: center;
212
+ justify-content: center;
213
+ width: 15px;
214
+ height: 15px;
215
+ margin-left: 4px;
216
+ cursor: pointer;
217
+ background-color: transparent;
218
+ border-radius: 50%;
219
+ transition: background-color 0.1s cubic-bezier(0, 0, 1, 1);
220
+
221
+ .close-icon {
222
+ z-index: 9;
223
+ width: 11px;
224
+ height: 11px;
225
+ color: inherit;
226
+ }
227
+
228
+ /* 主题色写在 .gi-tag__color--* 上,自定义色写在内联 style;统一用变量驱动悬停 */
229
+ &:hover {
230
+ color: var(--tag-close-hover-color);
231
+ background-color: var(--tag-close-hover-bg-color);
232
+ }
233
+ }
234
+
235
+ .#{a.$prefix}-tag__size--small {
236
+ height: $tag-size-small-height;
237
+ padding: $tag-size-small-padding;
238
+
239
+ .#{a.$prefix}-tag__icon {
240
+ :deep(.el-icon),
241
+ :deep(svg) {
242
+ width: 10px;
243
+ height: 10px;
244
+ }
245
+ }
246
+
247
+ .#{a.$prefix}-tag-close-btn {
248
+ width: 14px;
249
+ height: 14px;
250
+
251
+ .close-icon {
252
+ width: 10px;
253
+ height: 10px;
254
+ }
255
+ }
256
+ }
257
+
258
+ .#{a.$prefix}-tag__size--default {
259
+ height: $tag-size-default-height;
260
+ padding: $tag-size-default-padding;
261
+ }
262
+
263
+ .#{a.$prefix}-tag__size--large {
264
+ height: $tag-size-large-height;
265
+ padding: $tag-size-large-padding;
266
+
267
+ .#{a.$prefix}-tag__icon {
268
+ :deep(.el-icon),
269
+ :deep(svg) {
270
+ width: 12px;
271
+ height: 12px;
272
+ }
273
+ }
274
+
275
+ .#{a.$prefix}-tag-close-btn {
276
+ width: 16px;
277
+ height: 16px;
278
+
279
+ .close-icon {
280
+ width: 12px;
281
+ height: 12px;
282
+ }
283
+ }
284
+ }
285
+
286
+ .#{a.$prefix}-tag__type--light {
287
+ @each $s in $theme-colors {
288
+ &.#{a.$prefix}-tag__color--#{$s} {
289
+ color: var(--el-color-#{$s});
290
+ background-color: var(--el-color-#{$s}-light-9);
291
+
292
+ --tag-close-hover-color: #fff;
293
+ --tag-close-hover-bg-color: var(--el-color-#{$s});
294
+ }
295
+ }
296
+ }
297
+
298
+ .#{a.$prefix}-tag__type--dark {
299
+ color: #fff;
300
+
301
+ @each $s in $theme-colors {
302
+ &.#{a.$prefix}-tag__color--#{$s} {
303
+ background-color: var(--el-color-#{$s});
304
+
305
+ --tag-close-hover-color: var(--el-color-#{$s});
306
+ --tag-close-hover-bg-color: rgb(255 255 255 / 90%);
307
+ }
308
+ }
309
+ }
310
+
311
+ .#{a.$prefix}-tag__type--outline {
312
+ background: transparent;
313
+ border-style: solid;
314
+ border-width: 1px;
315
+
316
+ @each $s in $theme-colors {
317
+ &.#{a.$prefix}-tag__color--#{$s} {
318
+ color: var(--el-color-#{$s});
319
+ border-color: var(--el-color-#{$s});
320
+
321
+ --tag-close-hover-color: #fff;
322
+ --tag-close-hover-bg-color: var(--el-color-#{$s});
323
+ }
324
+ }
325
+ }
326
+
327
+ .#{a.$prefix}-tag__type--light-outline {
328
+ border-style: solid;
329
+ border-width: 1px;
330
+
331
+ @each $s in $theme-colors {
332
+ &.#{a.$prefix}-tag__color--#{$s} {
333
+ color: var(--el-color-#{$s});
334
+ background-color: var(--el-color-#{$s}-light-9);
335
+ border-color: var(--el-color-#{$s}-light-5);
336
+
337
+ --tag-close-hover-color: #fff;
338
+ --tag-close-hover-bg-color: var(--el-color-#{$s});
339
+ }
340
+ }
341
+ }
342
+ </style>
@@ -0,0 +1,36 @@
1
+ import type { Component } from 'vue'
2
+
3
+ /** 与 Element Plus 语义色一致的主题色(走 CSS 变量,非内联色值) */
4
+ export type TagThemeColor = 'primary' | 'success' | 'warning' | 'danger' | 'info'
5
+
6
+ /** 内置调色板色名(映射为固定十六进制) */
7
+ export type TagPaletteColor =
8
+ | 'red'
9
+ | 'orangered'
10
+ | 'orange'
11
+ | 'gold'
12
+ | 'lime'
13
+ | 'green'
14
+ | 'cyan'
15
+ | 'blue'
16
+ | 'purple'
17
+ | 'pink'
18
+ | 'gray'
19
+
20
+ /** 组件属性定义 */
21
+ export interface TagProps {
22
+ /** 标签类型 */
23
+ type?: 'dark' | 'light' | 'outline' | 'light-outline'
24
+ /**
25
+ * 颜色:主题色名(primary / success 等,使用 Element 色板)、调色板预设名(red / blue 等)或任意 CSS 颜色字符串(如十六进制)
26
+ */
27
+ color?: TagThemeColor | TagPaletteColor | string
28
+ /** 标签尺寸 */
29
+ size?: 'small' | 'default' | 'large'
30
+ /**
31
+ * 左侧图标组件(如 `@element-plus/icons-vue` 图标);与 `#icon` 插槽同时存在时以插槽为准
32
+ */
33
+ icon?: Component
34
+ /** 是否可关闭 */
35
+ closable?: boolean
36
+ }
@@ -1,32 +1,33 @@
1
- /* eslint-disable */
2
- // @ts-nocheck
3
- // Generated by unplugin-vue-components
4
- // Read more: https://github.com/vuejs/core/pull/3399
5
- // biome-ignore lint: disable
6
- export {}
7
-
8
- /* prettier-ignore */
9
- declare module 'vue' {
10
- export interface GlobalComponents {
11
- GiButton: typeof import('./components/button/src/button.vue')['default']
12
- GiCard: typeof import('./components/card/src/card.vue')['default']
13
- GiDialog: typeof import('./components/dialog/src/dialog.vue')['default']
14
- GiDialogContent: typeof import('./components/dialog/src/dialog-content.vue')['default']
15
- GiDescriptions: typeof import('./components/descriptions/src/descriptions.vue')['default']
16
- GiDot: typeof import('./components/dot/src/dot.vue')['default']
17
- GiDrawer: typeof import('./components/drawer/src/drawer.vue')['default']
18
- GiEditTable: typeof import('./components/edit-table/src/edit-table.vue')['default']
19
- GiFlex: typeof import('./components/flex/src/flex.vue')['default']
20
- GiForm: typeof import('./components/form/src/form.vue')['default']
21
- GiGrid: typeof import('./components/grid/src/grid.vue')['default']
22
- GiGridItem: typeof import('./components/grid/src/grid-item.vue')['default']
23
- GiInputGroup: typeof import('./components/input-group/src/input-group.vue')['default']
24
- GiInputSearch: typeof import('./components/input-search/src/input-search.vue')['default']
25
- GiPageLayout: typeof import('./components/page-layout/src/page-layout.vue')['default']
26
- GiSplitButton: typeof import('./components/page-layout/src/split-button.vue')['default']
27
- GiTable: typeof import('./components/table/src/table.vue')['default']
28
- GiTableColumn: typeof import('./components/table/src/TableColumn.vue')['default']
29
- GiTabs: typeof import('./components/tabs/src/tabs.vue')['default']
30
- GiTreeTransfer: typeof import('./components/tree-transfer/src/tree-transfer.vue')['default']
31
- }
32
- }
1
+ /* eslint-disable */
2
+ // @ts-nocheck
3
+ // Generated by unplugin-vue-components
4
+ // Read more: https://github.com/vuejs/core/pull/3399
5
+ // biome-ignore lint: disable
6
+ export {}
7
+
8
+ /* prettier-ignore */
9
+ declare module 'vue' {
10
+ export interface GlobalComponents {
11
+ GiButton: typeof import('./components/button/src/button.vue')['default']
12
+ GiCard: typeof import('./components/card/src/card.vue')['default']
13
+ GiDescriptions: typeof import('./components/descriptions/src/descriptions.vue')['default']
14
+ GiDialog: typeof import('./components/dialog/src/dialog.vue')['default']
15
+ GiDialogContent: typeof import('./components/dialog/src/dialog-content.vue')['default']
16
+ GiDot: typeof import('./components/dot/src/dot.vue')['default']
17
+ GiDrawer: typeof import('./components/drawer/src/drawer.vue')['default']
18
+ GiEditTable: typeof import('./components/edit-table/src/edit-table.vue')['default']
19
+ GiFlex: typeof import('./components/flex/src/flex.vue')['default']
20
+ GiForm: typeof import('./components/form/src/form.vue')['default']
21
+ GiGrid: typeof import('./components/grid/src/grid.vue')['default']
22
+ GiGridItem: typeof import('./components/grid/src/grid-item.vue')['default']
23
+ GiInputGroup: typeof import('./components/input-group/src/input-group.vue')['default']
24
+ GiInputSearch: typeof import('./components/input-search/src/input-search.vue')['default']
25
+ GiPageLayout: typeof import('./components/page-layout/src/page-layout.vue')['default']
26
+ GiSplitButton: typeof import('./components/page-layout/src/split-button.vue')['default']
27
+ GiTable: typeof import('./components/table/src/table.vue')['default']
28
+ GiTableColumn: typeof import('./components/table/src/TableColumn.vue')['default']
29
+ GiTabs: typeof import('./components/tabs/src/tabs.vue')['default']
30
+ GiTag: typeof import('./components/tag/src/tag.vue')['default']
31
+ GiTreeTransfer: typeof import('./components/tree-transfer/src/tree-transfer.vue')['default']
32
+ }
33
+ }
package/packages/index.ts CHANGED
@@ -16,6 +16,7 @@ import InputSearch from './components/input-search'
16
16
  import PageLayout from './components/page-layout'
17
17
  import Table from './components/table'
18
18
  import Tabs from './components/tabs'
19
+ import Tag from './components/tag'
19
20
  import TreeTransfer from './components/tree-transfer'
20
21
  import './styles/index.scss'
21
22
 
@@ -29,6 +30,7 @@ export * from './components/edit-table'
29
30
  export * from './components/form'
30
31
  export * from './components/table'
31
32
  export * from './components/tabs'
33
+ export * from './components/tag'
32
34
  export * from './hooks'
33
35
  export * from './utils'
34
36
 
@@ -49,6 +51,7 @@ const components = {
49
51
  Dialog: DialogComponent,
50
52
  EditTable,
51
53
  Table,
54
+ Tag,
52
55
  TreeTransfer
53
56
  } as unknown as Record<string, Component>
54
57
 
@@ -69,6 +72,7 @@ export const GiPageLayout: typeof PageLayout = PageLayout
69
72
  export const GiDialog: typeof DialogComponent = DialogComponent
70
73
  export const GiEditTable: typeof EditTable = EditTable
71
74
  export const GiTable: typeof Table = Table
75
+ export const GiTag: typeof Tag = Tag
72
76
  export const GiTreeTransfer: typeof TreeTransfer = TreeTransfer
73
77
 
74
78
  function capitalizeWord(word: string) {