-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate new component
Tippy
with demo (#5355)
* 添加新的工具提示组件Tippy
- Loading branch information
Showing
12 changed files
with
462 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
packages/effects/common-ui/src/components/tippy/directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import type { ComputedRef, Directive } from 'vue'; | ||
|
||
import { useTippy } from 'vue-tippy'; | ||
|
||
export default function useTippyDirective(isDark: ComputedRef<boolean>) { | ||
const directive: Directive = { | ||
mounted(el, binding, vnode) { | ||
const opts = | ||
typeof binding.value === 'string' | ||
? { content: binding.value } | ||
: binding.value || {}; | ||
|
||
const modifiers = Object.keys(binding.modifiers || {}); | ||
const placement = modifiers.find((modifier) => modifier !== 'arrow'); | ||
const withArrow = modifiers.includes('arrow'); | ||
|
||
if (placement) { | ||
opts.placement = opts.placement || placement; | ||
} | ||
|
||
if (withArrow) { | ||
opts.arrow = opts.arrow === undefined ? true : opts.arrow; | ||
} | ||
|
||
if (vnode.props && vnode.props.onTippyShow) { | ||
opts.onShow = function (...args: any[]) { | ||
return vnode.props?.onTippyShow(...args); | ||
}; | ||
} | ||
|
||
if (vnode.props && vnode.props.onTippyShown) { | ||
opts.onShown = function (...args: any[]) { | ||
return vnode.props?.onTippyShown(...args); | ||
}; | ||
} | ||
|
||
if (vnode.props && vnode.props.onTippyHidden) { | ||
opts.onHidden = function (...args: any[]) { | ||
return vnode.props?.onTippyHidden(...args); | ||
}; | ||
} | ||
|
||
if (vnode.props && vnode.props.onTippyHide) { | ||
opts.onHide = function (...args: any[]) { | ||
return vnode.props?.onTippyHide(...args); | ||
}; | ||
} | ||
|
||
if (vnode.props && vnode.props.onTippyMount) { | ||
opts.onMount = function (...args: any[]) { | ||
return vnode.props?.onTippyMount(...args); | ||
}; | ||
} | ||
|
||
if (el.getAttribute('title') && !opts.content) { | ||
opts.content = el.getAttribute('title'); | ||
el.removeAttribute('title'); | ||
} | ||
|
||
if (el.getAttribute('content') && !opts.content) { | ||
opts.content = el.getAttribute('content'); | ||
} | ||
|
||
useTippy(el, opts); | ||
}, | ||
unmounted(el) { | ||
if (el.$tippy) { | ||
el.$tippy.destroy(); | ||
} else if (el._tippy) { | ||
el._tippy.destroy(); | ||
} | ||
}, | ||
|
||
updated(el, binding) { | ||
const opts = | ||
typeof binding.value === 'string' | ||
? { content: binding.value, theme: isDark.value ? '' : 'light' } | ||
: Object.assign( | ||
{ theme: isDark.value ? '' : 'light' }, | ||
binding.value, | ||
); | ||
|
||
if (el.getAttribute('title') && !opts.content) { | ||
opts.content = el.getAttribute('title'); | ||
el.removeAttribute('title'); | ||
} | ||
|
||
if (el.getAttribute('content') && !opts.content) { | ||
opts.content = el.getAttribute('content'); | ||
} | ||
|
||
if (el.$tippy) { | ||
el.$tippy.setProps(opts || {}); | ||
} else if (el._tippy) { | ||
el._tippy.setProps(opts || {}); | ||
} | ||
}, | ||
}; | ||
return directive; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import type { DefaultProps, Props } from 'tippy.js'; | ||
|
||
import type { App, SetupContext } from 'vue'; | ||
|
||
import { h, watchEffect } from 'vue'; | ||
import { setDefaultProps, Tippy as TippyComponent } from 'vue-tippy'; | ||
|
||
import { usePreferences } from '@vben-core/preferences'; | ||
|
||
import useTippyDirective from './directive'; | ||
|
||
import 'tippy.js/dist/tippy.css'; | ||
import 'tippy.js/themes/light.css'; | ||
import 'tippy.js/animations/scale.css'; | ||
import 'tippy.js/animations/scale-subtle.css'; | ||
import 'tippy.js/animations/scale-extreme.css'; | ||
import 'tippy.js/animations/shift-away.css'; | ||
import 'tippy.js/animations/perspective.css'; | ||
|
||
const { isDark } = usePreferences(); | ||
export type TippyProps = Props & { | ||
animation?: | ||
| 'fade' | ||
| 'perspective' | ||
| 'scale' | ||
| 'scale-extreme' | ||
| 'scale-subtle' | ||
| 'shift-away' | ||
| boolean; | ||
theme?: 'auto' | 'dark' | 'light'; | ||
}; | ||
|
||
export function initTippy(app: App<Element>, options?: DefaultProps) { | ||
setDefaultProps({ | ||
allowHTML: true, | ||
delay: [500, 200], | ||
theme: isDark.value ? '' : 'light', | ||
...options, | ||
}); | ||
if (!options || !Reflect.has(options, 'theme') || options.theme === 'auto') { | ||
watchEffect(() => { | ||
setDefaultProps({ theme: isDark.value ? '' : 'light' }); | ||
}); | ||
} | ||
|
||
app.directive('tippy', useTippyDirective(isDark)); | ||
} | ||
|
||
export const Tippy = (props: any, { attrs, slots }: SetupContext) => { | ||
let theme: string = (attrs.theme as string) ?? 'auto'; | ||
if (theme === 'auto') { | ||
theme = isDark.value ? '' : 'light'; | ||
} | ||
if (theme === 'dark') { | ||
theme = ''; | ||
} | ||
return h( | ||
TippyComponent, | ||
{ | ||
...props, | ||
...attrs, | ||
theme, | ||
}, | ||
slots, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.