index.ts 994 B

1234567891011121314151617181920212223242526272829
  1. import { pascalCase } from 'scule'
  2. type Slots
  3. = 'header-start' | 'header-after-logo' | 'header-after-menu' | 'header-end'
  4. | 'main-sidebar-top' | 'main-sidebar-after-logo' | 'main-sidebar-after-menu' | 'main-sidebar-bottom'
  5. | 'sub-sidebar-top' | 'sub-sidebar-after-logo' | 'sub-sidebar-after-menu' | 'sub-sidebar-bottom'
  6. | 'tabbar-start' | 'tabbar-end'
  7. | 'toolbar-start' | 'toolbar-end'
  8. | 'free-position'
  9. function tryLoadComponent(name: Slots) {
  10. const componentMap = import.meta.glob('./*/index.vue', { eager: true })
  11. const path = `./${pascalCase(name as unknown as string)}/index.vue`
  12. const component = componentMap[path as keyof typeof componentMap]
  13. if (!component) {
  14. return {
  15. default: defineComponent({
  16. name: 'SlotsInvalidComponent',
  17. render: () => null,
  18. }),
  19. }
  20. }
  21. return component
  22. }
  23. export function useSlots(name: Slots) {
  24. const component = tryLoadComponent(name)
  25. return defineComponent((component as any).default)
  26. }