wd-config-provider.vue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <!--
  2. * @Author: weisheng
  3. * @Date: 2023-06-13 11:34:35
  4. * @LastEditTime: 2025-04-28 22:26:25
  5. * @LastEditors: weisheng
  6. * @Description:
  7. * @FilePath: /wot-design-uni/src/uni_modules/wot-design-uni/components/wd-config-provider/wd-config-provider.vue
  8. * 记得注释
  9. -->
  10. <template>
  11. <view :class="themeClass" :style="themeStyle">
  12. <slot />
  13. </view>
  14. </template>
  15. <script lang="ts">
  16. export default {
  17. name: 'wd-config-provider',
  18. options: {
  19. virtualHost: true,
  20. addGlobalClass: true,
  21. styleIsolation: 'shared'
  22. }
  23. }
  24. </script>
  25. <script lang="ts" setup>
  26. import { computed } from 'vue'
  27. import { configProviderProps } from './types'
  28. import { objToStyle } from '../common/util'
  29. const props = defineProps(configProviderProps)
  30. const themeClass = computed(() => {
  31. return `wot-theme-${props.theme} ${props.customClass}`
  32. })
  33. const themeStyle = computed(() => {
  34. const styleObj = mapThemeVarsToCSSVars(props.themeVars)
  35. return styleObj ? `${objToStyle(styleObj)}${props.customStyle}` : props.customStyle
  36. })
  37. const kebabCase = (str: string): string => {
  38. str = str.replace(str.charAt(0), str.charAt(0).toLocaleLowerCase())
  39. return str.replace(/([a-z])([A-Z])/g, (_, p1, p2) => p1 + '-' + p2.toLowerCase())
  40. }
  41. const colorRgb = (str: string) => {
  42. if (!str) return
  43. var sColor = str.toLowerCase()
  44. //十六进制颜色值的正则表达式
  45. var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
  46. // 如果是16进制颜色
  47. if (sColor && reg.test(sColor)) {
  48. if (sColor.length === 4) {
  49. var sColorNew = '#'
  50. for (let i = 1; i < 4; i += 1) {
  51. sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1))
  52. }
  53. sColor = sColorNew
  54. }
  55. //处理六位的颜色值
  56. var sColorChange: number[] = []
  57. for (let i = 1; i < 7; i += 2) {
  58. sColorChange.push(parseInt('0x' + sColor.slice(i, i + 2)))
  59. }
  60. return sColorChange.join(',')
  61. }
  62. return null
  63. }
  64. const mapThemeVarsToCSSVars = (themeVars: Record<string, string>) => {
  65. if (!themeVars) return
  66. const cssVars: Record<string, string> = {}
  67. Object.keys(themeVars).forEach((key) => {
  68. cssVars[`--wot-${kebabCase(key)}`] = themeVars[key]
  69. })
  70. return cssVars
  71. }
  72. </script>
  73. <style lang="scss" scoped></style>