|
| 1 | +import { TinyColor } from '@ctrl/tinycolor'; |
| 2 | +import { computed, watch } from 'vue'; |
| 3 | +import { theme } from '../../../components'; |
| 4 | +import { useConfigContextInject } from '../../../components/config-provider/context'; |
| 5 | + |
| 6 | +const { useToken } = theme; |
| 7 | + |
| 8 | +const useSiteToken = () => { |
| 9 | + const result = useToken(); |
| 10 | + const { getPrefixCls, iconPrefixCls } = useConfigContextInject(); |
| 11 | + const rootPrefixCls = computed(() => getPrefixCls()); |
| 12 | + const { token } = result; |
| 13 | + |
| 14 | + const mergedToken = computed(() => ({ |
| 15 | + theme: result.theme.value, |
| 16 | + hashId: result.hashId.value, |
| 17 | + token: { |
| 18 | + ...token.value, |
| 19 | + headerHeight: 64, |
| 20 | + menuItemBorder: 2, |
| 21 | + mobileMaxWidth: 767.99, |
| 22 | + siteMarkdownCodeBg: token.value.colorFillTertiary, |
| 23 | + antCls: `.${rootPrefixCls.value}`, |
| 24 | + iconCls: `.${iconPrefixCls.value}`, |
| 25 | + /** 56 */ |
| 26 | + marginFarXS: (token.value.marginXXL / 6) * 7, |
| 27 | + /** 80 */ |
| 28 | + marginFarSM: (token.value.marginXXL / 3) * 5, |
| 29 | + /** 96 */ |
| 30 | + marginFar: token.value.marginXXL * 2, |
| 31 | + codeFamily: `'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace`, |
| 32 | + }, |
| 33 | + })); |
| 34 | + let styleDom: HTMLStyleElement | null = null; |
| 35 | + watch( |
| 36 | + mergedToken, |
| 37 | + () => { |
| 38 | + styleDom = styleDom || document.createElement('style'); |
| 39 | + const tokenValue = mergedToken.value.token; |
| 40 | + const demoGridColor = token.colorPrimary; |
| 41 | + styleDom.innerHTML = ` |
| 42 | + :root { |
| 43 | + --header-height: ${tokenValue.headerHeight}px; |
| 44 | + --menu-item-border: ${tokenValue.menuItemBorder}px; |
| 45 | + --mobile-max-width: ${tokenValue.mobileMaxWidth}px; |
| 46 | +
|
| 47 | + --primary-color: ${tokenValue.colorPrimary}; |
| 48 | + --component-background: ${tokenValue.colorBgContainer}; |
| 49 | + --body-background: ${tokenValue.colorBgContainer}; |
| 50 | + --site-text-color: ${tokenValue.colorText}; |
| 51 | + --demo-grid-color: ${demoGridColor}; |
| 52 | + --demo-grid-color-65: ${new TinyColor(demoGridColor).setAlpha(0.75).toHex8String()}; |
| 53 | + --demo-grid-color-75: ${new TinyColor(demoGridColor).setAlpha(0.75).toHex8String()}; |
| 54 | + --site-text-color-secondary: ${tokenValue.colorTextSecondary}; |
| 55 | + --site-border-color-split: ${tokenValue.colorSplit}; |
| 56 | + --border-radius-base: ${tokenValue.borderRadius}; |
| 57 | + --font-size-base: ${tokenValue.fontSize}; |
| 58 | + --font-size-max: ${Math.max(tokenValue.fontSize - 1, 12)}px; |
| 59 | + --font-family: ${tokenValue.fontFamily}; |
| 60 | + --code-family: ${tokenValue.codeFamily}; |
| 61 | +
|
| 62 | + --border-color-base: ${tokenValue.colorBorder}; |
| 63 | + --purple-3: ${tokenValue['purple-3']}; |
| 64 | + --purple-6: ${tokenValue['purple-6']}; |
| 65 | + --text-color: ${tokenValue.colorText}; |
| 66 | + --search-icon-color: #ced4d9; |
| 67 | + --ease-in-out-circ: ${tokenValue.motionEaseInOutCirc}; |
| 68 | + --shadow-2: ${tokenValue.boxShadowSecondary}; |
| 69 | + --link-color: ${tokenValue.colorLink}; |
| 70 | + --error-color: ${tokenValue.colorError}; |
| 71 | + --white: ${tokenValue.colorWhite}; |
| 72 | + --green-6: ${tokenValue['green-6']}; |
| 73 | + --gray-8: ${tokenValue['gray-8']}; |
| 74 | + --magenta-7: ${tokenValue['magenta-7']}; |
| 75 | + --line-height-base: ${tokenValue.lineHeight}; |
| 76 | + --screen-md-min: ${tokenValue.screenMDMin}px; |
| 77 | + --screen-sm-min: ${tokenValue.screenSMMin}px; |
| 78 | + --screen-lg: ${tokenValue.screenLG}px; |
| 79 | + --mobile-max-width: ${tokenValue.mobileMaxWidth}px; |
| 80 | + --box-shadow-base: ${tokenValue.boxShadow} |
| 81 | + --animation-duration-base: ${tokenValue.motionDurationSlow}; |
| 82 | + --ease-in-out: ${tokenValue.motionEaseInOut}; |
| 83 | + --shadow-1-down: ${tokenValue.boxShadowCard}; |
| 84 | + } |
| 85 | + `; |
| 86 | + if (styleDom && !document.body.contains(styleDom)) { |
| 87 | + document.body.appendChild(styleDom); |
| 88 | + } |
| 89 | + }, |
| 90 | + { immediate: true }, |
| 91 | + ); |
| 92 | + |
| 93 | + return mergedToken; |
| 94 | +}; |
| 95 | + |
| 96 | +export default useSiteToken; |
0 commit comments