/src/components/Root.vue

https://github.com/egoist/docute · Vue · 59 lines · 56 code · 3 blank · 0 comment · 3 complexity · ed2d331346f8a6788de0cc9eb9b3ef1c MD5 · raw file

  1. <script>
  2. export default {
  3. name: 'DocuteRoot',
  4. render(h) {
  5. return h(
  6. 'div',
  7. {
  8. attrs: {
  9. id: this.$store.getters.target,
  10. class: 'Root'
  11. }
  12. },
  13. [h('router-view')]
  14. )
  15. },
  16. created() {
  17. this.insertStyle()
  18. },
  19. computed: {
  20. css() {
  21. const {cssVariables} = this.$store.getters
  22. const content = Object.keys(cssVariables).reduce((res, key) => {
  23. res += `--${key.replace(/[A-Z]/g, m => `-${m.toLowerCase()}`)}:${
  24. cssVariables[key]
  25. };`
  26. return res
  27. }, '')
  28. return `:root{${content}}`
  29. }
  30. },
  31. watch: {
  32. css() {
  33. this.insertStyle()
  34. }
  35. },
  36. methods: {
  37. insertStyle() {
  38. if (this.$ssrContext) {
  39. this.$ssrContext.insertedStyle = this.css
  40. return
  41. }
  42. const ID = 'docute-inserted-style'
  43. let style = document.getElementById(ID)
  44. if (style) {
  45. style.innerHTML = this.css
  46. } else {
  47. style = document.createElement('style')
  48. style.id = ID
  49. style.innerHTML = this.css
  50. document.head.insertBefore(style, document.head.firstChild)
  51. }
  52. }
  53. }
  54. }
  55. </script>
  56. <style src="../css/global.css"></style>