ConfigProvider.mjs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { watch, provide, computed, watchEffect, onActivated, onDeactivated, onBeforeUnmount, defineComponent, createVNode as _createVNode } from "vue";
  2. import { extend, inBrowser, kebabCase, makeStringProp, createNamespace } from "../utils/index.mjs";
  3. import { setGlobalZIndex } from "../composables/use-global-z-index.mjs";
  4. const [name, bem] = createNamespace("config-provider");
  5. const CONFIG_PROVIDER_KEY = Symbol(name);
  6. const configProviderProps = {
  7. tag: makeStringProp("div"),
  8. theme: makeStringProp("light"),
  9. zIndex: Number,
  10. themeVars: Object,
  11. themeVarsDark: Object,
  12. themeVarsLight: Object,
  13. themeVarsScope: makeStringProp("local"),
  14. iconPrefix: String
  15. };
  16. function insertDash(str) {
  17. return str.replace(/([a-zA-Z])(\d)/g, "$1-$2");
  18. }
  19. function mapThemeVarsToCSSVars(themeVars) {
  20. const cssVars = {};
  21. Object.keys(themeVars).forEach((key) => {
  22. const formattedKey = insertDash(kebabCase(key));
  23. cssVars[`--van-${formattedKey}`] = themeVars[key];
  24. });
  25. return cssVars;
  26. }
  27. function syncThemeVarsOnRoot(newStyle = {}, oldStyle = {}) {
  28. Object.keys(newStyle).forEach((key) => {
  29. if (newStyle[key] !== oldStyle[key]) {
  30. document.documentElement.style.setProperty(key, newStyle[key]);
  31. }
  32. });
  33. Object.keys(oldStyle).forEach((key) => {
  34. if (!newStyle[key]) {
  35. document.documentElement.style.removeProperty(key);
  36. }
  37. });
  38. }
  39. var stdin_default = defineComponent({
  40. name,
  41. props: configProviderProps,
  42. setup(props, {
  43. slots
  44. }) {
  45. const style = computed(() => mapThemeVarsToCSSVars(extend({}, props.themeVars, props.theme === "dark" ? props.themeVarsDark : props.themeVarsLight)));
  46. if (inBrowser) {
  47. const addTheme = () => {
  48. document.documentElement.classList.add(`van-theme-${props.theme}`);
  49. };
  50. const removeTheme = (theme = props.theme) => {
  51. document.documentElement.classList.remove(`van-theme-${theme}`);
  52. };
  53. watch(() => props.theme, (newVal, oldVal) => {
  54. if (oldVal) {
  55. removeTheme(oldVal);
  56. }
  57. addTheme();
  58. }, {
  59. immediate: true
  60. });
  61. onActivated(addTheme);
  62. onDeactivated(removeTheme);
  63. onBeforeUnmount(removeTheme);
  64. watch(style, (newStyle, oldStyle) => {
  65. if (props.themeVarsScope === "global") {
  66. syncThemeVarsOnRoot(newStyle, oldStyle);
  67. }
  68. });
  69. watch(() => props.themeVarsScope, (newScope, oldScope) => {
  70. if (oldScope === "global") {
  71. syncThemeVarsOnRoot({}, style.value);
  72. }
  73. if (newScope === "global") {
  74. syncThemeVarsOnRoot(style.value, {});
  75. }
  76. });
  77. if (props.themeVarsScope === "global") {
  78. syncThemeVarsOnRoot(style.value, {});
  79. }
  80. }
  81. provide(CONFIG_PROVIDER_KEY, props);
  82. watchEffect(() => {
  83. if (props.zIndex !== void 0) {
  84. setGlobalZIndex(props.zIndex);
  85. }
  86. });
  87. return () => _createVNode(props.tag, {
  88. "class": bem(),
  89. "style": props.themeVarsScope === "local" ? style.value : void 0
  90. }, {
  91. default: () => {
  92. var _a;
  93. return [(_a = slots.default) == null ? void 0 : _a.call(slots)];
  94. }
  95. });
  96. }
  97. });
  98. export {
  99. CONFIG_PROVIDER_KEY,
  100. configProviderProps,
  101. stdin_default as default
  102. };