mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-20 15:01:46 +01:00
feat(Chip): new component (#886)
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
This commit is contained in:
92
src/runtime/components/elements/Chip.vue
Normal file
92
src/runtime/components/elements/Chip.vue
Normal file
@@ -0,0 +1,92 @@
|
||||
<template>
|
||||
<div :class="ui.wrapper" v-bind="attrs">
|
||||
<slot />
|
||||
|
||||
<span v-if="show" :class="chipClass">
|
||||
<slot name="content">
|
||||
{{ text }}
|
||||
</slot>
|
||||
</span>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
import { defineComponent, computed, toRef } from 'vue'
|
||||
import type { PropType } from 'vue'
|
||||
import { twJoin } from 'tailwind-merge'
|
||||
import { useUI } from '../../composables/useUI'
|
||||
import { mergeConfig } from '../../utils'
|
||||
import type { ChipSize, ChipColor, ChipPosition, Strategy } from '../../types'
|
||||
// @ts-expect-error
|
||||
import appConfig from '#build/app.config'
|
||||
import { chip } from '#ui/ui.config'
|
||||
|
||||
const config = mergeConfig<typeof chip>(appConfig.ui.strategy, appConfig.ui.chip, chip)
|
||||
|
||||
export default defineComponent({
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
size: {
|
||||
type: String as PropType<ChipSize>,
|
||||
default: () => config.default.size,
|
||||
validator (value: string) {
|
||||
return Object.keys(config.size).includes(value)
|
||||
}
|
||||
},
|
||||
color: {
|
||||
type: String as PropType<ChipColor>,
|
||||
default: () => config.default.color,
|
||||
validator (value: string) {
|
||||
return ['gray', ...appConfig.ui.colors].includes(value)
|
||||
}
|
||||
},
|
||||
position: {
|
||||
type: String as PropType<ChipPosition>,
|
||||
default: () => config.default.position,
|
||||
validator (value: string) {
|
||||
return Object.keys(config.position).includes(value)
|
||||
}
|
||||
},
|
||||
text: {
|
||||
type: [String, Number],
|
||||
default: null
|
||||
},
|
||||
inset: {
|
||||
type: Boolean,
|
||||
default: () => config.default.inset
|
||||
},
|
||||
show: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
},
|
||||
class: {
|
||||
type: [String, Object, Array] as PropType<any>,
|
||||
default: undefined
|
||||
},
|
||||
ui: {
|
||||
type: Object as PropType<Partial<typeof config & { strategy?: Strategy }>>,
|
||||
default: undefined
|
||||
}
|
||||
},
|
||||
setup (props) {
|
||||
const { ui, attrs } = useUI('chip', toRef(props, 'ui'), config, toRef(props, 'class'))
|
||||
|
||||
const chipClass = computed(() => {
|
||||
return twJoin(
|
||||
ui.value.base,
|
||||
ui.value.size[props.size],
|
||||
ui.value.position[props.position],
|
||||
props.inset ? null : ui.value.translate[props.position],
|
||||
ui.value.background.replaceAll('{color}', props.color)
|
||||
)
|
||||
})
|
||||
|
||||
return {
|
||||
// eslint-disable-next-line vue/no-dupe-keys
|
||||
ui,
|
||||
attrs,
|
||||
chipClass
|
||||
}
|
||||
}
|
||||
})
|
||||
</script>
|
||||
11
src/runtime/types/chip.d.ts
vendored
Normal file
11
src/runtime/types/chip.d.ts
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
import { chip } from '../ui.config'
|
||||
import colors from '#ui-colors'
|
||||
|
||||
export type ChipSize = keyof typeof chip.size
|
||||
export type ChipColor = 'gray' | typeof colors[number]
|
||||
export type ChipPosition = keyof typeof chip.position
|
||||
|
||||
export interface Chip {
|
||||
color?: ChipColor
|
||||
position?: ChipPosition
|
||||
}
|
||||
1
src/runtime/types/index.d.ts
vendored
1
src/runtime/types/index.d.ts
vendored
@@ -4,6 +4,7 @@ export * from './avatar'
|
||||
export * from './badge'
|
||||
export * from './breadcrumb'
|
||||
export * from './button'
|
||||
export * from './chip'
|
||||
export * from './clipboard'
|
||||
export * from './command-palette'
|
||||
export * from './dropdown'
|
||||
|
||||
@@ -257,6 +257,41 @@ export const buttonGroup = {
|
||||
shadow: 'shadow-sm'
|
||||
}
|
||||
|
||||
export const chip = {
|
||||
wrapper: 'relative inline-flex items-center justify-center flex-shrink-0',
|
||||
base: 'absolute rounded-full ring-1 ring-white dark:ring-gray-900 flex items-center justify-center text-white dark:text-gray-900 font-medium whitespace-nowrap',
|
||||
background: 'bg-{color}-500 dark:bg-{color}-400',
|
||||
position: {
|
||||
'top-right': 'top-0 right-0',
|
||||
'bottom-right': 'bottom-0 right-0',
|
||||
'top-left': 'top-0 left-0',
|
||||
'bottom-left': 'bottom-0 left-0'
|
||||
},
|
||||
translate: {
|
||||
'top-right': '-translate-y-1/2 translate-x-1/2 transform',
|
||||
'bottom-right': 'translate-y-1/2 translate-x-1/2 transform',
|
||||
'top-left': '-translate-y-1/2 -translate-x-1/2 transform',
|
||||
'bottom-left': 'translate-y-1/2 -translate-x-1/2 transform'
|
||||
},
|
||||
size: {
|
||||
'3xs': 'h-[4px] min-w-[4px] text-[4px] p-px',
|
||||
'2xs': 'h-[5px] min-w-[5px] text-[5px] p-px',
|
||||
xs: 'h-1.5 min-w-[0.375rem] text-[6px] p-px',
|
||||
sm: 'h-2 min-w-[0.5rem] text-[7px] p-0.5',
|
||||
md: 'h-2.5 min-w-[0.625rem] text-[8px] p-0.5',
|
||||
lg: 'h-3 min-w-[0.75rem] text-[10px] p-0.5',
|
||||
xl: 'h-3.5 min-w-[0.875rem] text-[11px] p-1',
|
||||
'2xl': 'h-4 min-w-[1rem] text-[12px] p-1',
|
||||
'3xl': 'h-5 min-w-[1.25rem] text-[14px] p-1'
|
||||
},
|
||||
default: {
|
||||
size: 'sm',
|
||||
color: 'primary',
|
||||
position: 'top-right',
|
||||
inset: false
|
||||
}
|
||||
}
|
||||
|
||||
export const dropdown = {
|
||||
wrapper: 'relative inline-flex text-left rtl:text-right',
|
||||
container: 'z-20 group',
|
||||
|
||||
Reference in New Issue
Block a user