diff --git a/src/code/index.js b/src/code/index.js deleted file mode 100644 index abe37fb4f..000000000 --- a/src/code/index.js +++ /dev/null @@ -1,2 +0,0 @@ -/* istanbul ignore file */ -export { default as NCode } from './src/Code.js' diff --git a/src/code/index.ts b/src/code/index.ts new file mode 100644 index 000000000..1bc9795f6 --- /dev/null +++ b/src/code/index.ts @@ -0,0 +1,2 @@ +/* istanbul ignore file */ +export { default as NCode } from './src/Code' diff --git a/src/code/src/Code.js b/src/code/src/Code.ts similarity index 68% rename from src/code/src/Code.js rename to src/code/src/Code.ts index d4c8a7273..afabdc029 100644 --- a/src/code/src/Code.js +++ b/src/code/src/Code.ts @@ -1,16 +1,8 @@ -import { - defineComponent, - h, - nextTick, - toRef, - watch, - onMounted, - ref, - computed -} from 'vue' +import { defineComponent, h, toRef, watch, onMounted, ref, computed } from 'vue' import { useTheme, useHljs } from '../../_mixins' import { codeLight } from '../styles' -import style from './styles/index.cssr.js' +import type { CodeThemeVars } from '../styles' +import style from './styles/index.cssr' export default defineComponent({ name: 'Code', @@ -34,37 +26,43 @@ export default defineComponent({ } }, setup (props, { slots }) { - const codeRef = ref(null) + const codeRef = ref(null) const hljsRef = useHljs(props) - const generateCodeHTML = (language, code, trim) => { + const createCodeHtml = (language: string, code: string, trim: boolean) => { const { value: hljs } = hljsRef - const languageValid = !!(language && hljs.getLanguage(language)) - if (trim) code = code.trim() - return { - valid: languageValid, - content: hljs.highlight(language, code).value + if (!hljs) { + return null } + if (!(language && hljs.getLanguage(language))) { + return null + } + return hljs.highlight(language, trim ? code.trim() : code).value } const setCode = () => { if (slots.default) return + const { value: codeEl } = codeRef + if (!codeEl) return const { code, language } = props if (language) { - const { valid, content } = generateCodeHTML(language, code, props.trim) - if (valid) { - codeRef.value.innerHTML = content + const html = createCodeHtml(language, code, props.trim) + if (html !== null) { + codeEl.innerHTML = html return } } - codeRef.value.textContent = code + codeEl.textContent = code } onMounted(setCode) - watch(toRef(props, 'language'), () => { - nextTick(setCode) - }) - watch(toRef(props, 'code'), () => { - nextTick(setCode) - }) - const themeRef = useTheme('Code', 'Code', style, codeLight, props) + watch(toRef(props, 'language'), setCode) + watch(toRef(props, 'code'), setCode) + watch(hljsRef, setCode) + const themeRef = useTheme( + 'Code', + 'Code', + style, + codeLight, + props + ) return { codeRef, cssVars: computed(() => { diff --git a/src/code/src/styles/index.cssr.js b/src/code/src/styles/index.cssr.ts similarity index 91% rename from src/code/src/styles/index.cssr.js rename to src/code/src/styles/index.cssr.ts index a3f25b207..f06b30253 100644 --- a/src/code/src/styles/index.cssr.js +++ b/src/code/src/styles/index.cssr.ts @@ -18,23 +18,30 @@ const codeClass = withPrefix('code') // --hue-6 // --hue-6-2 export default c([ - cB('code', ` + cB( + 'code', + ` display: block; font-size: var(--font-size); font-family: var(--font-family); - `, [ - c('pre', ` + `, + [ + c( + 'pre', + ` margin: 0; font-family: inherit; - `), - c('[class^=hljs]', { - color: 'var(--text-color)', - transition: ` + ` + ), + c('[class^=hljs]', { + color: 'var(--text-color)', + transition: ` color .3s var(--bezier), background-color .3s var(--bezier) ` - }) - ]), + }) + ] + ), `${codeClass} .hljs-comment, ${codeClass} .hljs-quote { color: var(--mono-3); diff --git a/src/code/styles/dark.js b/src/code/styles/dark.ts similarity index 77% rename from src/code/styles/dark.js rename to src/code/styles/dark.ts index 38f5e0838..e38a866db 100644 --- a/src/code/styles/dark.js +++ b/src/code/styles/dark.ts @@ -1,9 +1,11 @@ import { commonDark } from '../../_styles/new-common' +import type { ThemeCommonVars } from '../../_styles/new-common' +import type { CodeThemeVars } from './light' export default { name: 'Code', common: commonDark, - self (vars) { + self (vars: ThemeCommonVars): CodeThemeVars { const { textColor2, fontSize, fontWeightStrong } = vars return { textColor: textColor2, diff --git a/src/code/styles/index.js b/src/code/styles/index.js deleted file mode 100644 index 194a0083d..000000000 --- a/src/code/styles/index.js +++ /dev/null @@ -1,2 +0,0 @@ -export { default as codeDark } from './dark.js' -export { default as codeLight } from './light.js' diff --git a/src/code/styles/index.ts b/src/code/styles/index.ts new file mode 100644 index 000000000..6535a2f89 --- /dev/null +++ b/src/code/styles/index.ts @@ -0,0 +1,3 @@ +export { default as codeDark } from './dark' +export { default as codeLight } from './light' +export type { CodeThemeVars } from './light' diff --git a/src/code/styles/light.js b/src/code/styles/light.ts similarity index 72% rename from src/code/styles/light.js rename to src/code/styles/light.ts index 4e7ea5d06..561710ed4 100644 --- a/src/code/styles/light.js +++ b/src/code/styles/light.ts @@ -1,9 +1,10 @@ import { commonLight } from '../../_styles/new-common' +import type { ThemeCommonVars } from '../../_styles/new-common' -export default { +const codeLight = { name: 'Code', common: commonLight, - self (vars) { + self (vars: ThemeCommonVars) { const { textColor2, fontSize, fontWeightStrong } = vars return { textColor: textColor2, @@ -22,3 +23,6 @@ export default { } } } + +export default codeLight +export type CodeThemeVars = ReturnType diff --git a/src/config-provider/src/ConfigProvider.ts b/src/config-provider/src/ConfigProvider.ts index 342af9a8f..657d542d1 100644 --- a/src/config-provider/src/ConfigProvider.ts +++ b/src/config-provider/src/ConfigProvider.ts @@ -21,11 +21,6 @@ import { export default defineComponent({ name: 'ConfigProvider', alias: ['App'], - provide () { - return { - NConfigProvider: this - } - }, props: { abstract: { type: Boolean, diff --git a/src/dropdown/src/Dropdown.ts b/src/dropdown/src/Dropdown.ts index c6226bb81..5139cf17c 100644 --- a/src/dropdown/src/Dropdown.ts +++ b/src/dropdown/src/Dropdown.ts @@ -10,12 +10,7 @@ import { reactive } from 'vue' import { RawNode, TreeMate } from 'treemate' -import { - useMergedState, - // useFalseUntilTruthy, - useKeyboard, - useMemo -} from 'vooks' +import { useMergedState, useKeyboard, useMemo } from 'vooks' import { useTheme } from '../../_mixins' import { NPopover, popoverProps } from '../../popover' import { keep, call, createKey } from '../../_utils' @@ -105,7 +100,6 @@ export default defineComponent({ toRef(props, 'show'), uncontrolledShowRef ) - // const dataNeededRef = useFalseUntilTruthy(mergedShowRef) const treemateRef = computed(() => { return TreeMate(props.options, treemateOptions) })