Skip to content

Commit

Permalink
Move dark-mode detection one level higher to mitigate performance issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkb committed Sep 13, 2024
1 parent da8bcf7 commit 7842ef0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
24 changes: 7 additions & 17 deletions frontend/src/components/VIcon/VIcon.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<script setup lang="ts">
import { ref, watch, computed } from "vue"
import { ref, watch } from "vue"
import { useSprite } from "~/composables/use-sprite"
import { useDarkMode } from "~/composables/use-dark-mode"
/**
* Vendored from SVG Sprite Module https://github.com/nuxt-modules/svg-sprite
* The module had problem with handling the source code that is inside `/src` directory.
Expand Down Expand Up @@ -54,8 +52,6 @@ const props = withDefaults(
}
)
const { effectiveColorMode } = useDarkMode()
if (!props.viewBox.split(" ").every((v) => !isNaN(parseInt(v)))) {
throw new Error(
`Invalid viewBox "${props.viewBox}" for icon "${props.name}".`
Expand All @@ -70,20 +66,14 @@ const icon = ref({
class: "",
})
const iconName = computed(() => {
let name = props.name.includes("/") ? props.name : `icons/${props.name}`
if (name.startsWith("licenses")) {
name = `${name}-${effectiveColorMode.value}`
}
return name
})
icon.value = useSprite(iconName.value)
icon.value = useSprite(
props.name.includes("/") ? props.name : `icons/${props.name}`
)
watch(
() => [props.name, effectiveColorMode.value],
() => {
icon.value = useSprite(iconName.value)
() => props.name,
(name) => {
icon.value = useSprite(name)
}
)
</script>
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/components/VLicense/VLicense.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { useI18n } from "#imports"
import { computed } from "vue"
import { useDarkMode } from "~/composables/use-dark-mode"
import type { License } from "~/constants/license"
import { getFullLicenseName, getElements } from "~/utils/license"
import { camelCase } from "~/utils/case"
Expand Down Expand Up @@ -35,6 +37,8 @@ const props = withDefaults(
}
)
const { effectiveColorMode } = useDarkMode()
const i18n = useI18n({ useScope: "global" })
const iconNames = computed(() => getElements(props.license))
Expand All @@ -56,7 +60,7 @@ const licenseName = computed(() => {
:key="name"
:class="{ 'license-bg text-black': bgFilled }"
view-box="0 0 30 30"
:name="`licenses/${name}`"
:name="`licenses/${name}-${effectiveColorMode}`"
:size="4"
/>
</div>
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/components/VLicense/VLicenseElements.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useI18n } from "#imports"
import { computed } from "vue"
import type { License } from "~/constants/license"
import { useDarkMode } from "~/composables/use-dark-mode"
import { useUiStore } from "~/stores/ui"
import { camelCase } from "~/utils/case"
import { getElements } from "~/utils/license"
Expand All @@ -27,6 +28,8 @@ const props = withDefaults(
}
)
const { effectiveColorMode } = useDarkMode()
const i18n = useI18n({ useScope: "global" })
const elementNames = computed(() =>
getElements(props.license).filter((icon) => icon !== "cc")
Expand All @@ -51,7 +54,7 @@ const getLicenseDescription = (element: string) => {
<VIcon
view-box="0 0 30 30"
:size="isSmall || isMobile ? 5 : 6"
:name="`licenses/${element}`"
:name="`licenses/${element}-${effectiveColorMode}`"
/>
<span v-if="elementNames.length > 1" class="sr-only">{{
element.toUpperCase()
Expand Down

0 comments on commit 7842ef0

Please sign in to comment.