feat(code): css in js (#236)

This commit is contained in:
HaiboTang 2020-08-07 21:17:02 +08:00 committed by 07akioni
parent 8fd1f9a295
commit e2389f0d0e
10 changed files with 415 additions and 174 deletions

View File

@ -1,8 +0,0 @@
/* istanbul ignore file */
import NCode from './src/main.vue'
NCode.install = function (Vue) {
Vue.component(NCode.name, NCode)
}
export default NCode

View File

@ -1,176 +1,176 @@
@import "./mixins/mixins.scss"; // @import "./mixins/mixins.scss";
@include themes-mixin { // @include themes-mixin {
@include b(code) { // @include b(code) {
[class^=hljs] { // [class^=hljs] {
color: $--code-text-color; // color: $--code-text-color;
} // }
} // }
} // }
@include b(code) { // @include b(code) {
margin: 0; // margin: 0;
font-size: 14px; // font-size: 14px;
font-family: $--n-mono-font-family; // font-family: $--n-mono-font-family;
code { // code {
font-family: $--n-mono-font-family; // font-family: $--n-mono-font-family;
} // }
[class^=hljs] { // [class^=hljs] {
transition: color .3s $--n-ease-in-out-cubic-bezier, background-color .3s $--n-ease-in-out-cubic-bezier; // transition: color .3s $--n-ease-in-out-cubic-bezier, background-color .3s $--n-ease-in-out-cubic-bezier;
} // }
&.#{block(light-theme)} { // &.#{block(light-theme)} {
.hljs { // .hljs {
display: block; // display: block;
overflow-x: auto; // overflow-x: auto;
padding: 0.5em; // padding: 0.5em;
background: #fafafa; // background: #fafafa;
} // }
.hljs-comment, // .hljs-comment,
.hljs-quote { // .hljs-quote {
color: #a0a1a7; // color: #a0a1a7;
font-style: italic; // font-style: italic;
} // }
.hljs-doctag, // .hljs-doctag,
.hljs-keyword, // .hljs-keyword,
.hljs-formula { // .hljs-formula {
color: #a626a4; // color: #a626a4;
} // }
.hljs-section, // .hljs-section,
.hljs-name, // .hljs-name,
.hljs-selector-tag, // .hljs-selector-tag,
.hljs-deletion, // .hljs-deletion,
.hljs-subst { // .hljs-subst {
color: #e45649; // color: #e45649;
} // }
.hljs-literal { // .hljs-literal {
color: #0184bb; // color: #0184bb;
} // }
.hljs-string, // .hljs-string,
.hljs-regexp, // .hljs-regexp,
.hljs-addition, // .hljs-addition,
.hljs-attribute, // .hljs-attribute,
.hljs-meta-string { // .hljs-meta-string {
color: #50a14f; // color: #50a14f;
} // }
.hljs-built_in, // .hljs-built_in,
.hljs-class .hljs-title { // .hljs-class .hljs-title {
color: #c18401; // color: #c18401;
} // }
.hljs-attr, // .hljs-attr,
.hljs-variable, // .hljs-variable,
.hljs-template-variable, // .hljs-template-variable,
.hljs-type, // .hljs-type,
.hljs-selector-class, // .hljs-selector-class,
.hljs-selector-attr, // .hljs-selector-attr,
.hljs-selector-pseudo, // .hljs-selector-pseudo,
.hljs-number { // .hljs-number {
color: #986801; // color: #986801;
} // }
.hljs-symbol, // .hljs-symbol,
.hljs-bullet, // .hljs-bullet,
.hljs-link, // .hljs-link,
.hljs-meta, // .hljs-meta,
.hljs-selector-id, // .hljs-selector-id,
.hljs-title { // .hljs-title {
color: #4078f2; // color: #4078f2;
} // }
.hljs-emphasis { // .hljs-emphasis {
font-style: italic; // font-style: italic;
} // }
.hljs-strong { // .hljs-strong {
font-weight: $--n-strong-weight; // font-weight: $--n-strong-weight;
} // }
.hljs-link { // .hljs-link {
text-decoration: underline; // text-decoration: underline;
} // }
.hljs-function .hljs-params { // .hljs-function .hljs-params {
color: #383a42; // color: #383a42;
} // }
.hljs-function .hljs-params .hljs-typing { // .hljs-function .hljs-params .hljs-typing {
color: #383a42; // color: #383a42;
} // }
} // }
&.#{block(dark-theme)} { // &.#{block(dark-theme)} {
.hljs { // .hljs {
display: block; // display: block;
overflow-x: auto; // overflow-x: auto;
padding: 0.5em; // padding: 0.5em;
line-height: 1.3em; // line-height: 1.3em;
color: #abb2bf; // color: #abb2bf;
background: #282c34; // background: #282c34;
} // }
.hljs-keyword, .hljs-operator { // .hljs-keyword, .hljs-operator {
color: #F92672; // color: #F92672;
} // }
.hljs-pattern-match { // .hljs-pattern-match {
color: #F92672; // color: #F92672;
} // }
.hljs-pattern-match .hljs-constructor { // .hljs-pattern-match .hljs-constructor {
color: #61aeee; // color: #61aeee;
} // }
.hljs-function { // .hljs-function {
color: #61aeee; // color: #61aeee;
} // }
.hljs-function .hljs-params { // .hljs-function .hljs-params {
color: #A6E22E; // color: #A6E22E;
} // }
.hljs-function .hljs-params .hljs-typing { // .hljs-function .hljs-params .hljs-typing {
color: #FD971F; // color: #FD971F;
} // }
.hljs-module-access .hljs-module { // .hljs-module-access .hljs-module {
color: #7e57c2; // color: #7e57c2;
} // }
.hljs-constructor { // .hljs-constructor {
color: #e2b93d; // color: #e2b93d;
} // }
.hljs-constructor .hljs-string { // .hljs-constructor .hljs-string {
color: #9CCC65; // color: #9CCC65;
} // }
.hljs-comment, .hljs-quote { // .hljs-comment, .hljs-quote {
color: #b18eb1; // color: #b18eb1;
font-style: italic; // font-style: italic;
} // }
.hljs-doctag, .hljs-formula { // .hljs-doctag, .hljs-formula {
color: #c678dd; // color: #c678dd;
} // }
.hljs-section, .hljs-name, .hljs-selector-tag, .hljs-deletion, .hljs-subst { // .hljs-section, .hljs-name, .hljs-selector-tag, .hljs-deletion, .hljs-subst {
color: #e06c75; // color: #e06c75;
} // }
.hljs-literal { // .hljs-literal {
color: #56b6c2; // color: #56b6c2;
} // }
.hljs-string, .hljs-regexp, .hljs-addition, .hljs-attribute, .hljs-meta-string { // .hljs-string, .hljs-regexp, .hljs-addition, .hljs-attribute, .hljs-meta-string {
color: #98c379; // color: #98c379;
} // }
.hljs-built_in, .hljs-class .hljs-title { // .hljs-built_in, .hljs-class .hljs-title {
color: #e6c07b; // color: #e6c07b;
} // }
.hljs-attr, .hljs-variable, .hljs-template-variable, .hljs-type, .hljs-selector-class, .hljs-selector-attr, .hljs-selector-pseudo, .hljs-number { // .hljs-attr, .hljs-variable, .hljs-template-variable, .hljs-type, .hljs-selector-class, .hljs-selector-attr, .hljs-selector-pseudo, .hljs-number {
color: #d19a66; // color: #d19a66;
} // }
.hljs-symbol, .hljs-bullet, .hljs-link, .hljs-meta, .hljs-selector-id, .hljs-title { // .hljs-symbol, .hljs-bullet, .hljs-link, .hljs-meta, .hljs-selector-id, .hljs-title {
color: #61aeee; // color: #61aeee;
} // }
.hljs-emphasis { // .hljs-emphasis {
font-style: italic; // font-style: italic;
} // }
.hljs-strong { // .hljs-strong {
font-weight: $--n-strong-weight; // font-weight: $--n-strong-weight;
} // }
.hljs-link { // .hljs-link {
text-decoration: underline; // text-decoration: underline;
} // }
} // }
} // }

View File

@ -53,4 +53,4 @@ function cTB (selector, ...rest) {
]) ])
} }
export { c, cTB, cB, cE, cM, cNotM, insideFormItem, insideModal, namespace } export { c, cTB, cB, cE, cM, cNotM, insideFormItem, insideModal, namespace, prefix }

8
src/code/index.js Normal file
View File

@ -0,0 +1,8 @@
/* istanbul ignore file */
import NCode from './src/Code.vue'
NCode.install = function (Vue, naive) {
Vue.component(naive.componentPrefix + NCode.name, NCode)
}
export default NCode

View File

@ -1,10 +1,12 @@
<script> <script>
import withapp from '../../_mixins/withapp' import withapp from '../../_mixins/withapp'
import themeable from '../../_mixins/themeable' import themeable from '../../_mixins/themeable'
import usecssr from '../../_mixins/usecssr'
import styles from './styles/index.js'
export default { export default {
name: 'NCode', name: 'Code',
mixins: [ withapp, themeable ], mixins: [withapp, themeable, usecssr(styles)],
props: { props: {
language: { language: {
type: String, type: String,

View File

@ -0,0 +1,11 @@
import baseStyle from './themed-base.cssr.js'
export default [
{
key: 'syntheticTheme',
watch: [
'syntheticTheme'
],
CNode: baseStyle
}
]

View File

@ -0,0 +1,196 @@
import { c, cTB, cB, prefix } from '../../../_utils/cssr'
export default c([
({ props }) => {
const local = props.$local
const { monoFontFamily, easeInOutCubicBezier, strongFontWeight } = props.$base
const {
codeTextColor
} = local
return [
cTB(
'code',
[
c('[class^=hljs]', {
color: codeTextColor
})
]),
cB('code', {
raw: `
margin: 0;
font-size: 14px;
font-family: ${monoFontFamily};
`
},
[
c('code', {
fontFamily: monoFontFamily
}),
c('[class^=hljs]', {
transition: `
color .3s ${easeInOutCubicBezier},
background-color .3s ${easeInOutCubicBezier}
`
}),
c(`&${prefix}light-theme`,
[
c('hljs', {
raw: `
display: block;
overflow-x: auto;
padding: 0.5em;
background: #fafafa;
`
}),
c('.hljs-comment, .hljs-quote', {
raw: `
color: #a0a1a7;
font-style: italic;
`
}),
c('.hljs-doctag, .hljs-keyword, .hljs-formula', {
raw: `
color: #a626a4;
`
}),
c(`.hljs-section,
.hljs-name,
.hljs-selector-tag,
.hljs-deletion,
.hljs-subst`, {
raw: `
color: #e45649;
`
}),
c('.hljs-literal', {
raw: `
color: #0184bb;
`
}),
c(`.hljs-string,
.hljs-regexp,
.hljs-addition,
.hljs-attribute,
.hljs-meta-string`, {
color: `#50a14f`
}),
c(`.hljs-built_in,
.hljs-class .hljs-title`, {
color: `#c18401`
}),
c(`.hljs-attr,
.hljs-variable,
.hljs-template-variable,
.hljs-type,
.hljs-selector-class,
.hljs-selector-attr,
.hljs-selector-pseudo,
.hljs-number`, {
color: `#986801`
}),
c(`.hljs-symbol,
.hljs-bullet,
.hljs-link,
.hljs-meta,
.hljs-selector-id,
.hljs-title`, {
color: `#4078f2`
}),
c('.hljs-emphasis', {
fontStyle: `italic`
}),
c('.hljs-strong', {
fontWeight: strongFontWeight
}),
c('.hljs-link', {
textDecoration: 'underline'
}),
c(`.hljs-function .hljs-params, .hljs-function .hljs-params .hljs-typing`, {
color: `#383a42`
})
]),
c(`&${prefix}dark-theme`,
[
c('hljs', {
raw: `
display: block;
overflow-x: auto;
padding: 0.5em;
line-height: 1.3em;
color: #abb2bf;
background: #282c34;
`
}),
c('.hljs-comment, .hljs-quote', {
raw: `
color: #61aeee;
font-style: italic;
`
}),
c('.hljs-pattern-match, .hljs-keyword, .hljs-operator', {
raw: `
color: #F92672;
`
}),
c(`.hljs-section,
.hljs-name,
.hljs-selector-tag,
.hljs-deletion,
.hljs-subst`, {
raw: `
color: #A6E22E;
`
}),
c('.hljs-literal', {
raw: `
color: #FD971F;
`
}),
c(`.hljs-string,
.hljs-regexp,
.hljs-addition,
.hljs-attribute,
.hljs-meta-string`, {
color: `#7e57c2`
}),
c(`.hljs-built_in,
.hljs-class .hljs-title`, {
color: `#e2b93d`
}),
c(`.hljs-attr,
.hljs-variable,
.hljs-template-variable,
.hljs-type,
.hljs-selector-class,
.hljs-selector-attr,
.hljs-selector-pseudo,
.hljs-number`, {
color: `#9CCC65`
}),
c(`.hljs-symbol,
.hljs-bullet,
.hljs-link,
.hljs-meta,
.hljs-selector-id,
.hljs-title`, {
color: `#b18eb1`
}),
c('.hljs-emphasis', {
fontStyle: `italic`
}),
c('.hljs-strong', {
fontWeight: strongFontWeight
}),
c('.hljs-link', {
textDecoration: 'underline'
}),
c(`.hljs-function .hljs-params, .hljs-function .hljs-params .hljs-typing`, {
color: `#e06c75`
})
])
])
]
}
])

14
src/code/styles/dark.js Normal file
View File

@ -0,0 +1,14 @@
import create from '../../styles/_utils/create-component-base'
export default create({
theme: 'dark',
name: 'Code',
getDerivedVariables ({ base, derived }) {
const {
secondaryTextColor
} = derived
return {
codeTextColor: secondaryTextColor
}
}
})

14
src/code/styles/light.js Normal file
View File

@ -0,0 +1,14 @@
import create from '../../styles/_utils/create-component-base'
export default create({
theme: 'light',
name: 'Code',
getDerivedVariables ({ base, derived }) {
const {
secondaryTextColor
} = derived
return {
codeTextColor: secondaryTextColor
}
}
})

View File

@ -62,7 +62,7 @@ import AutoComplete from './auto-complete'
import Empty from './Empty' import Empty from './Empty'
import Element from './element' import Element from './element'
import Log from './Log' import Log from './Log'
import Code from './Code' import Code from './code'
import Typography from './typography' import Typography from './typography'
import Upload from './Upload' import Upload from './Upload'
import InputGroup from './input-group' import InputGroup from './input-group'
@ -111,6 +111,8 @@ import backTopLightStyle from './back-top/styles/light'
import backTopDarkStyle from './back-top/styles/dark' import backTopDarkStyle from './back-top/styles/dark'
import autoComplateLightStyle from './auto-complete/styles/light' import autoComplateLightStyle from './auto-complete/styles/light'
import autoComplateDarkStyle from './auto-complete/styles/dark' import autoComplateDarkStyle from './auto-complete/styles/dark'
import codeLightStyle from './code/styles/light'
import codeDarkStyle from './code/styles/dark'
// Can be remove after refactoring // Can be remove after refactoring
import baseSelectionLightStyle from './_base/selection/styles/light' import baseSelectionLightStyle from './_base/selection/styles/light'
@ -244,6 +246,8 @@ export default create({
autoComplateDarkStyle, autoComplateDarkStyle,
backTopLightStyle, backTopLightStyle,
backTopDarkStyle, backTopDarkStyle,
codeLightStyle,
codeDarkStyle,
// Can be remove after refactoring // Can be remove after refactoring
baseSelectionLightStyle, baseSelectionLightStyle,
baseSelectionDarkStyle baseSelectionDarkStyle