createTailwindClasses.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { generateClasses } from '@formkit/themes'
  3. import { extend } from '@formkit/utils'
  4. import type {
  5. FormAppSpecificTheme,
  6. FormThemeClasses,
  7. FormThemeExtension,
  8. } from '#shared/types/form.ts'
  9. import type {
  10. ImportGlobEagerOutput,
  11. ImportGlobEagerDefault,
  12. } from '#shared/types/utils.ts'
  13. import commonClasses from '../theme/global/index.ts'
  14. const extensionsModules: ImportGlobEagerOutput<FormThemeExtension> =
  15. import.meta.glob('../theme/global/extensions/*.ts', { eager: true })
  16. const getExtensionsFromModules = (
  17. extensionsModules: ImportGlobEagerOutput<FormThemeExtension>,
  18. ) => {
  19. const extensions: FormThemeExtension[] = []
  20. Object.values(extensionsModules).forEach(
  21. (module: ImportGlobEagerDefault<FormThemeExtension>) => {
  22. const extension = module.default
  23. extensions.push(extension)
  24. },
  25. )
  26. return extensions
  27. }
  28. const createTailwindClasses = (additionalTheme: FormAppSpecificTheme = {}) => {
  29. const themeExtensions: FormThemeExtension[] = [
  30. ...getExtensionsFromModules(extensionsModules),
  31. ]
  32. if (additionalTheme.coreClasses) {
  33. themeExtensions.push(additionalTheme.coreClasses)
  34. }
  35. if (additionalTheme.extensions) {
  36. themeExtensions.push(
  37. ...getExtensionsFromModules(additionalTheme.extensions),
  38. )
  39. }
  40. let classes = commonClasses
  41. themeExtensions.forEach((extension) => {
  42. const newClasses = extension(classes)
  43. classes = extend(classes, newClasses) as FormThemeClasses
  44. })
  45. return generateClasses(classes)
  46. }
  47. export default createTailwindClasses