refactor(tooltip): ts

This commit is contained in:
07akioni 2021-01-16 00:25:28 +08:00
parent 963991b97a
commit b08ac196bf
14 changed files with 71 additions and 38 deletions

View File

@ -26,20 +26,20 @@ type UseThemeProps<T> = Readonly<{
[key: string]: unknown [key: string]: unknown
}> }>
export interface MergedTheme<T> { export interface MergedTheme<T, R = any> {
common: ThemeCommonVars common: ThemeCommonVars
self: T self: T
peers: any peers: R
overrides: any overrides: any
} }
function useTheme<T> ( function useTheme<T, R> (
resolveId: string, resolveId: string,
mountId: string, mountId: string,
style: CNode | undefined, style: CNode | undefined,
defaultTheme: Theme<T>, defaultTheme: Theme<T, R>,
props: UseThemeProps<T> props: UseThemeProps<T>
): ComputedRef<MergedTheme<T>> { ): ComputedRef<MergedTheme<T, R>> {
if (style) { if (style) {
onBeforeMount(() => { onBeforeMount(() => {
style.mount({ style.mount({

View File

@ -7,7 +7,7 @@ const dropdownDark: DropdownTheme = {
name: 'Dropdown', name: 'Dropdown',
common: commonDark, common: commonDark,
peers: { peers: {
NPopover: popoverDark Popover: popoverDark
}, },
self (vars) { self (vars) {
const { const {

View File

@ -52,13 +52,13 @@ export type DropdownThemeVars = ReturnType<typeof self>
const dropdownLight: Theme< const dropdownLight: Theme<
DropdownThemeVars, DropdownThemeVars,
{ {
NPopover: PopoverTheme Popover?: PopoverTheme
} }
> = { > = {
name: 'Dropdown', name: 'Dropdown',
common: commonLight, common: commonLight,
peers: { peers: {
NPopover: popoverLight Popover: popoverLight
}, },
self self
} }

View File

@ -1 +1,2 @@
export { default as NPopover, popoverProps } from './src/Popover' export { default as NPopover, popoverProps } from './src/Popover'
export type { PopoverRef } from './src/Popover'

View File

@ -64,6 +64,10 @@ const textVNodeType = createTextVNode('').type
type BodyInstance = { syncPosition: () => void; [key: string]: unknown } type BodyInstance = { syncPosition: () => void; [key: string]: unknown }
export interface PopoverRef {
syncPosition(): void
}
export interface PopoverInjection { export interface PopoverInjection {
handleMouseLeave: (e: MouseEvent) => void handleMouseLeave: (e: MouseEvent) => void
handleMouseEnter: (e: MouseEvent) => void handleMouseEnter: (e: MouseEvent) => void

View File

@ -1 +0,0 @@
export { default as NTooltip } from './src/Tooltip.js'

1
src/tooltip/index.ts Normal file
View File

@ -0,0 +1 @@
export { default as NTooltip } from './src/Tooltip'

View File

@ -1,25 +1,35 @@
// Tooltip: popover wearing waistcoat // Tooltip: popover wearing waistcoat
import { h, defineComponent, ref, computed } from 'vue' import { h, defineComponent, ref, computed } from 'vue'
import { NPopover } from '../../popover' import { NPopover, popoverProps } from '../../popover'
import type { PopoverRef } from '../../popover'
import { useTheme } from '../../_mixins' import { useTheme } from '../../_mixins'
import type { ThemeProps } from '../../_mixins'
import { tooltipLight } from '../styles' import { tooltipLight } from '../styles'
import type { TooltipTheme } from '../styles'
export default defineComponent({ export default defineComponent({
name: 'Tooltip', name: 'Tooltip',
props: { props: {
...NPopover.props, ...popoverProps,
...(useTheme.props as ThemeProps<TooltipTheme>),
showArrow: { showArrow: {
type: Boolean, type: Boolean,
default: false default: false
} }
}, },
setup (props) { setup (props) {
const themeRef = useTheme('Tooltip', 'Tooltip', null, tooltipLight, props) const themeRef = useTheme(
const popoverRef = ref(null) 'Tooltip',
'Tooltip',
undefined,
tooltipLight,
props
)
const popoverRef = ref<PopoverRef | null>(null)
return { return {
popoverRef, popoverRef,
syncPosition () { syncPosition () {
popoverRef.value.syncPosition() (popoverRef.value as PopoverRef).syncPosition()
}, },
mergedTheme: themeRef, mergedTheme: themeRef,
popoverThemeOverrides: computed(() => { popoverThemeOverrides: computed(() => {

View File

@ -1,8 +1,9 @@
import { commonDark } from '../../_styles/new-common' import { commonDark } from '../../_styles/new-common'
import { popoverDark } from '../../popover/styles' import { popoverDark } from '../../popover/styles'
import commonVars from './_common' import commonVars from './_common'
import type { TooltipTheme } from './light'
export default { const tooltipDark: TooltipTheme = {
name: 'Tooltip', name: 'Tooltip',
common: commonDark, common: commonDark,
peers: { peers: {
@ -19,3 +20,5 @@ export default {
} }
} }
} }
export default tooltipDark

View File

@ -1,2 +0,0 @@
export { default as tooltipDark } from './dark.js'
export { default as tooltipLight } from './light.js'

View File

@ -0,0 +1,3 @@
export { default as tooltipDark } from './dark'
export { default as tooltipLight } from './light'
export type { TooltipThemeVars, TooltipTheme } from './light'

View File

@ -1,21 +0,0 @@
import { commonLight } from '../../_styles/new-common'
import { popoverLight } from '../../popover/styles'
import commonVars from './_common'
export default {
name: 'Tooltip',
common: commonLight,
peers: {
Popover: popoverLight
},
self (vars) {
const { borderRadius, boxShadow2, baseColor } = vars
return {
...commonVars,
borderRadius: borderRadius,
boxShadow: boxShadow2,
color: 'rgba(0, 0, 0, .85)',
textColor: baseColor
}
}
}

View File

@ -0,0 +1,35 @@
import { commonLight } from '../../_styles/new-common'
import type { ThemeCommonVars } from '../../_styles/new-common'
import { popoverLight, PopoverTheme } from '../../popover/styles'
import commonVars from './_common'
import type { Theme } from '../../_mixins/use-theme'
const self = (vars: ThemeCommonVars) => {
const { borderRadius, boxShadow2, baseColor } = vars
return {
...commonVars,
borderRadius: borderRadius,
boxShadow: boxShadow2,
color: 'rgba(0, 0, 0, .85)',
textColor: baseColor
}
}
export type TooltipThemeVars = ReturnType<typeof self>
const tooltipLight: Theme<
TooltipThemeVars,
{
Popover?: PopoverTheme
}
> = {
name: 'Tooltip',
common: commonLight,
peers: {
Popover: popoverLight
},
self
}
export default tooltipLight
export type TooltipTheme = typeof tooltipLight