-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace computed with ref and onMounted (#5237)
- Loading branch information
Showing
4 changed files
with
75 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { getElements } from "#imports" | ||
|
||
import { computed, onMounted, ref, watch } from "vue" | ||
|
||
import type { License } from "~/constants/license" | ||
import { useDarkMode } from "~/composables/use-dark-mode" | ||
|
||
/** | ||
* Generate the names for `VIcon` components based on the license and color mode. | ||
* The value is updated on mounted and when the color mode changes. | ||
* | ||
* This is necessary to prevent client-server mismatch: the server does not have access | ||
* to media queries, so the `system` color mode always defaults to "light". | ||
* @param license - the license to generate icons for | ||
* @param filterOutCc - whether to filter out the `cc` from the list of icons | ||
*/ | ||
export const useIconNames = ({ | ||
license, | ||
filterOutCc, | ||
}: { | ||
license: License | ||
filterOutCc: boolean | ||
}) => { | ||
const getIconNames = (elements: string[], colorMode: "dark" | "light") => { | ||
return elements.map((element) => `licenses/${element}-${colorMode}`) | ||
} | ||
const { effectiveColorMode, serverColorMode } = useDarkMode() | ||
|
||
const icons = computed(() => { | ||
const elements = getElements(license) | ||
return filterOutCc | ||
? elements.filter((element) => element !== "cc") | ||
: elements | ||
}) | ||
|
||
const iconNames = ref(getIconNames(icons.value, serverColorMode.value)) | ||
|
||
onMounted(() => { | ||
watch( | ||
effectiveColorMode, | ||
() => { | ||
iconNames.value = getIconNames(icons.value, effectiveColorMode.value) | ||
}, | ||
{ immediate: true } | ||
) | ||
}) | ||
|
||
return { iconNames } | ||
} |