Skip to content

Commit 105d642

Browse files
committed
perf(rating): use useColor instead of useLayer
1 parent 1ff29a5 commit 105d642

File tree

5 files changed

+38
-16
lines changed

5 files changed

+38
-16
lines changed

packages/anu-vue/src/components/rating/ARating.vue

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<script lang="ts" setup>
2+
import { defu } from 'defu'
23
import type { ExtractPropTypes } from 'vue'
3-
import { useLayer, useProps as useLayerProps } from '@/composables/useLayer'
4-
import { disabled as disabledProp, readonly as readonlyProp } from '@/composables/useProps'
4+
import { useColor } from '@/composables'
5+
import { color as colorProp, disabled as disabledProp, readonly as readonlyProp } from '@/composables/useProps'
56
67
const props = defineProps({
7-
...useLayerProps({
8-
color: {
9-
default: 'warning',
10-
},
11-
}),
8+
/**
9+
* Rating color
10+
*/
11+
color: defu({ default: 'warning' }, colorProp),
1212
1313
/**
1414
* Bind v-model value to rating
@@ -81,13 +81,7 @@ defineOptions({
8181
name: 'ARating',
8282
})
8383
84-
const { getLayerClasses } = useLayer()
85-
86-
const { styles, classes } = getLayerClasses(
87-
toRef(props, 'color'),
88-
ref(''),
89-
ref(false),
90-
)
84+
const { styles } = useColor(toRef(props, 'color'), 'rating-color')
9185
9286
const rating = ref(0)
9387
const isHovered = ref(false)
@@ -140,7 +134,6 @@ const onMouseLeave = () => {
140134
(props.animate && !props.readonly && !props.disabled) && 'a-rating-animated',
141135
props.readonly && 'a-rating-readonly pointer-events-none',
142136
props.disabled && 'a-rating-disabled pointer-events-none',
143-
...classes,
144137
]"
145138
>
146139
<i

packages/anu-vue/src/composables/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './useColor'
12
export * from './useConfigurable'
23
export * from './useDOMScrollLock'
34
export * from './useGroupModel'
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { MaybeRef } from '@vueuse/core'
2+
import type { StyleValue } from 'vue'
3+
import type { ColorProp } from '@/composables/useProps'
4+
5+
export const useColor = (color: MaybeRef<ColorProp>, cssVarName: MaybeRef<string>, as: 'text' | 'bg' = 'text') => {
6+
const styles = computed(() => {
7+
const _color = unref(color)
8+
const cssVar = computed(() => `--a-${unref(cssVarName)}`)
9+
10+
const property = as === 'bg' ? 'background-color' : 'color'
11+
const isThemeColor = ['primary', 'success', 'info', 'warning', 'danger'].includes(_color as string)
12+
13+
const _styles = {
14+
[cssVar.value]: isThemeColor ? `hsl(var(--a-${_color}))` : _color,
15+
[property]: `var(${cssVar.value})`,
16+
} as StyleValue
17+
18+
return _styles
19+
})
20+
21+
return {
22+
styles,
23+
}
24+
}

packages/anu-vue/src/composables/useLayer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface LayerProps {
1515
// Thanks: https://youtu.be/a_m7jxrTlaw
1616
// type LooseAutocomplete<T extends string> = T | Omit<string, T>
1717

18+
// TODO: Use `useColor` composable to removed the color calculation
1819
export const useProps = (propOverrides?: Partial<ComponentObjectPropsOptions>) => {
1920
let props = {
2021
/**

packages/anu-vue/src/composables/useProps.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ import { createDefu } from 'defu'
22
import type { PropType } from 'vue'
33
import type { ConfigurableValue } from '@/composables/useConfigurable'
44

5+
// ℹ️ We need to move this to some better places
6+
export type LooseAutocomplete<T extends string> = T | Omit<string, T>
7+
58
export const themeColors = ['primary', 'success', 'info', 'warning', 'danger'] as const
69
export type ThemeColor = typeof themeColors[number]
7-
export type ColorProp = ThemeColor | undefined
10+
export type ColorProp = LooseAutocomplete<ThemeColor> | undefined
811

912
export const color = {
1013
type: [String, undefined] as PropType<ColorProp>,

0 commit comments

Comments
 (0)