index.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import type { DefaultProps, Props } from 'tippy.js';
  2. import type { App, SetupContext } from 'vue';
  3. import { h, watchEffect } from 'vue';
  4. import { setDefaultProps, Tippy as TippyComponent } from 'vue-tippy';
  5. import { usePreferences } from '@vben-core/preferences';
  6. import useTippyDirective from './directive';
  7. import 'tippy.js/dist/tippy.css';
  8. import 'tippy.js/dist/backdrop.css';
  9. import 'tippy.js/themes/light.css';
  10. import 'tippy.js/animations/scale.css';
  11. import 'tippy.js/animations/shift-toward.css';
  12. import 'tippy.js/animations/shift-away.css';
  13. import 'tippy.js/animations/perspective.css';
  14. const { isDark } = usePreferences();
  15. export type TippyProps = Partial<
  16. Props & {
  17. animation?:
  18. | 'fade'
  19. | 'perspective'
  20. | 'scale'
  21. | 'shift-away'
  22. | 'shift-toward'
  23. | boolean;
  24. theme?: 'auto' | 'dark' | 'light';
  25. }
  26. >;
  27. export function initTippy(app: App<Element>, options?: DefaultProps) {
  28. setDefaultProps({
  29. allowHTML: true,
  30. delay: [500, 200],
  31. theme: isDark.value ? '' : 'light',
  32. ...options,
  33. });
  34. if (!options || !Reflect.has(options, 'theme') || options.theme === 'auto') {
  35. watchEffect(() => {
  36. setDefaultProps({ theme: isDark.value ? '' : 'light' });
  37. });
  38. }
  39. app.directive('tippy', useTippyDirective(isDark));
  40. }
  41. export const Tippy = (props: any, { attrs, slots }: SetupContext) => {
  42. let theme: string = (attrs.theme as string) ?? 'auto';
  43. if (theme === 'auto') {
  44. theme = isDark.value ? '' : 'light';
  45. }
  46. if (theme === 'dark') {
  47. theme = '';
  48. }
  49. return h(
  50. TippyComponent,
  51. {
  52. ...props,
  53. ...attrs,
  54. theme,
  55. },
  56. slots,
  57. );
  58. };