import { ExtractPropTypes } from 'vue' import { makeComponentProps } from '@/composables/component' import { IconValue, useIcon } from '@/composables/icons' import { makeSizeProps, useSize } from '@/composables/size' import { makeTagProps } from '@/composables/tag' import { getIconComponentName } from '@/utils' import './UIconStyles.scss' import { UIconParentContainer } from './UIconParentContainer' import { computed, ref, Text } from 'vue' import { convertToUnit, flattenFragments, genericComponent, propsFactory, useRender, } from '@/utils' export const makeUIconProps = propsFactory( { color: { type: String, default: 'primary-700', required: false }, icon: IconValue, isHover: Boolean, isActive: Boolean, /** * @variant = [default | lightCircle | lightCircleOutline | * darkCircle | lightSquare | midSquare | darkSquare] * */ variant: { type: String, default: 'default', required: false, }, ...makeComponentProps(), ...makeSizeProps(), ...makeTagProps({ tag: 'i' }), }, 'UIcon' ) export type UIconProps = ExtractPropTypes export const UIcon = genericComponent()({ name: 'UIcon', props: makeUIconProps(), setup(props, { attrs, slots }) { const slotIcon = ref() if (props.icon === '$undefined') { return useRender(() => <>{slots.default?.()}) } useRender(() => { const slotValue = slots.default?.() if (slotValue) { slotIcon.value = flattenFragments(slotValue).filter( (node) => node.type === Text && node.children && typeof node.children === 'string' )[0]?.children as string } const { iconData } = useIcon( computed(() => getIconComponentName((slotIcon.value || props.icon) as string) ) ) const { sizeClasses } = useSize(props) const iconColor = computed(() => { if ( props.variant === 'lightCircleOutline' || props.variant === 'lightCircle' || props.variant === 'lightSquare' || props.variant === 'default' ) { return props.color } else { return 'white' } }) return props.variant === 'default' ? ( {slotValue} ) : ( {slotValue} ) }) return {} }, }) export type UIcon = InstanceType