ConfigProvider.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name2 in all)
  7. __defProp(target, name2, { get: all[name2], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. CONFIG_PROVIDER_KEY: () => CONFIG_PROVIDER_KEY,
  21. configProviderProps: () => configProviderProps,
  22. default: () => stdin_default
  23. });
  24. module.exports = __toCommonJS(stdin_exports);
  25. var import_vue = require("vue");
  26. var import_utils = require("../utils");
  27. var import_use_global_z_index = require("../composables/use-global-z-index");
  28. const [name, bem] = (0, import_utils.createNamespace)("config-provider");
  29. const CONFIG_PROVIDER_KEY = Symbol(name);
  30. const configProviderProps = {
  31. tag: (0, import_utils.makeStringProp)("div"),
  32. theme: (0, import_utils.makeStringProp)("light"),
  33. zIndex: Number,
  34. themeVars: Object,
  35. themeVarsDark: Object,
  36. themeVarsLight: Object,
  37. themeVarsScope: (0, import_utils.makeStringProp)("local"),
  38. iconPrefix: String
  39. };
  40. function insertDash(str) {
  41. return str.replace(/([a-zA-Z])(\d)/g, "$1-$2");
  42. }
  43. function mapThemeVarsToCSSVars(themeVars) {
  44. const cssVars = {};
  45. Object.keys(themeVars).forEach((key) => {
  46. const formattedKey = insertDash((0, import_utils.kebabCase)(key));
  47. cssVars[`--van-${formattedKey}`] = themeVars[key];
  48. });
  49. return cssVars;
  50. }
  51. function syncThemeVarsOnRoot(newStyle = {}, oldStyle = {}) {
  52. Object.keys(newStyle).forEach((key) => {
  53. if (newStyle[key] !== oldStyle[key]) {
  54. document.documentElement.style.setProperty(key, newStyle[key]);
  55. }
  56. });
  57. Object.keys(oldStyle).forEach((key) => {
  58. if (!newStyle[key]) {
  59. document.documentElement.style.removeProperty(key);
  60. }
  61. });
  62. }
  63. var stdin_default = (0, import_vue.defineComponent)({
  64. name,
  65. props: configProviderProps,
  66. setup(props, {
  67. slots
  68. }) {
  69. const style = (0, import_vue.computed)(() => mapThemeVarsToCSSVars((0, import_utils.extend)({}, props.themeVars, props.theme === "dark" ? props.themeVarsDark : props.themeVarsLight)));
  70. if (import_utils.inBrowser) {
  71. const addTheme = () => {
  72. document.documentElement.classList.add(`van-theme-${props.theme}`);
  73. };
  74. const removeTheme = (theme = props.theme) => {
  75. document.documentElement.classList.remove(`van-theme-${theme}`);
  76. };
  77. (0, import_vue.watch)(() => props.theme, (newVal, oldVal) => {
  78. if (oldVal) {
  79. removeTheme(oldVal);
  80. }
  81. addTheme();
  82. }, {
  83. immediate: true
  84. });
  85. (0, import_vue.onActivated)(addTheme);
  86. (0, import_vue.onDeactivated)(removeTheme);
  87. (0, import_vue.onBeforeUnmount)(removeTheme);
  88. (0, import_vue.watch)(style, (newStyle, oldStyle) => {
  89. if (props.themeVarsScope === "global") {
  90. syncThemeVarsOnRoot(newStyle, oldStyle);
  91. }
  92. });
  93. (0, import_vue.watch)(() => props.themeVarsScope, (newScope, oldScope) => {
  94. if (oldScope === "global") {
  95. syncThemeVarsOnRoot({}, style.value);
  96. }
  97. if (newScope === "global") {
  98. syncThemeVarsOnRoot(style.value, {});
  99. }
  100. });
  101. if (props.themeVarsScope === "global") {
  102. syncThemeVarsOnRoot(style.value, {});
  103. }
  104. }
  105. (0, import_vue.provide)(CONFIG_PROVIDER_KEY, props);
  106. (0, import_vue.watchEffect)(() => {
  107. if (props.zIndex !== void 0) {
  108. (0, import_use_global_z_index.setGlobalZIndex)(props.zIndex);
  109. }
  110. });
  111. return () => (0, import_vue.createVNode)(props.tag, {
  112. "class": bem(),
  113. "style": props.themeVarsScope === "local" ? style.value : void 0
  114. }, {
  115. default: () => {
  116. var _a;
  117. return [(_a = slots.default) == null ? void 0 : _a.call(slots)];
  118. }
  119. });
  120. }
  121. });