aboutsummaryrefslogtreecommitdiff
path: root/src/components/contrast_ratio/contrast_ratio.vue
blob: bbd6fd4a5200e8c8a9405b17724611f6c7696a86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<template>
  <span
    v-if="contrast"
    class="contrast-ratio"
  >
    <span
      :title="hint"
      class="rating"
    >
      <span v-if="contrast.aaa">
        <FAIcon icon="thumbs-up" />
      </span>
      <span v-if="!contrast.aaa && contrast.aa">
        <FAIcon icon="adjust" />
      </span>
      <span v-if="!contrast.aaa && !contrast.aa">
        <FAIcon icon="exclamation-triangle" />
      </span>
    </span>
    <span
      v-if="contrast && large"
      class="rating"
      :title="hint_18pt"
    >
      <span v-if="contrast.laaa">
        <FAIcon icon="thumbs-up" />
      </span>
      <span v-if="!contrast.laaa && contrast.laa">
        <FAIcon icon="adjust" />
      </span>
      <span v-if="!contrast.laaa && !contrast.laa">
        <FAIcon icon="exclamation-triangle" />
      </span>
    </span>
  </span>
</template>

<script>
import { library } from '@fortawesome/fontawesome-svg-core'
import {
  faAdjust,
  faExclamationTriangle,
  faThumbsUp
} from '@fortawesome/free-solid-svg-icons'

library.add(
  faAdjust,
  faExclamationTriangle,
  faThumbsUp
)

export default {
  props: {
    large: {
      required: false,
      type: Boolean,
      default: false
    },
    // TODO: Make theme switcher compute theme initially so that contrast
    // component won't be called without contrast data
    contrast: {
      required: false,
      type: Object,
      default: () => ({})
    }
  },
  computed: {
    hint () {
      const levelVal = this.contrast.aaa ? 'aaa' : (this.contrast.aa ? 'aa' : 'bad')
      const level = this.$t(`settings.style.common.contrast.level.${levelVal}`)
      const context = this.$t('settings.style.common.contrast.context.text')
      const ratio = this.contrast.text
      return this.$t('settings.style.common.contrast.hint', { level, context, ratio })
    },
    hint_18pt () {
      const levelVal = this.contrast.laaa ? 'aaa' : (this.contrast.laa ? 'aa' : 'bad')
      const level = this.$t(`settings.style.common.contrast.level.${levelVal}`)
      const context = this.$t('settings.style.common.contrast.context.18pt')
      const ratio = this.contrast.text
      return this.$t('settings.style.common.contrast.hint', { level, context, ratio })
    }
  }
}
</script>

<style lang="scss">
.contrast-ratio {
  display: flex;
  justify-content: flex-end;
  margin-top: -4px;
  margin-bottom: 5px;

  .label {
    margin-right: 1em;
  }

  .rating {
    display: inline-block;
    text-align: center;
    margin-left: 0.5em;
  }
}
</style>