-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from nblackburn/feature/dynamic-palette
Dynamic palette
- Loading branch information
Showing
6 changed files
with
93 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<template> | ||
<slot /> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { onMounted } from 'vue'; | ||
import { getActivePalette, applyPalette } from '@utilities/paletteManager'; | ||
export default { | ||
setup() { | ||
onMounted(() => { | ||
const bodyElement = document.body; | ||
const activePalette = getActivePalette(); | ||
applyPalette(activePalette, bodyElement); | ||
}); | ||
} | ||
}; | ||
</script> |
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 was deleted.
Oops, something went wrong.
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,62 @@ | ||
import { getDate, getMonth } from 'date-fns'; | ||
import { | ||
paletteClasses, | ||
defaultPalette, | ||
christmasPalette, | ||
halloweenPalette | ||
} from '@styles/palettes.css'; | ||
|
||
export const getActivePalette = (): string => { | ||
const now = new Date(); | ||
const currentDate = getDate(now); | ||
const currentMonth = getMonth(now); | ||
|
||
if (currentDate === 31 && currentMonth === 9) { | ||
return halloweenPalette; | ||
} | ||
|
||
if (currentDate === 25 && currentMonth === 11) { | ||
return christmasPalette; | ||
} | ||
|
||
return defaultPalette; | ||
}; | ||
|
||
export const getAppliedPalettes = (element: HTMLElement): string[] => { | ||
const appliedPalettes: string[] = []; | ||
const classList = element.classList; | ||
|
||
// Figure out what palettes are currently applied. | ||
classList.forEach((elementClass) => { | ||
if (paletteClasses.includes(elementClass)) { | ||
appliedPalettes.push(elementClass); | ||
} | ||
}); | ||
|
||
return appliedPalettes; | ||
}; | ||
|
||
export const applyPalette = (palette: string, element: HTMLElement): void => { | ||
const classList = element.classList; | ||
const appliedPalettes = getAppliedPalettes(element); | ||
|
||
// Check we don't already have the target palette applied. | ||
if (appliedPalettes.includes(palette)) { | ||
return; | ||
} | ||
|
||
// Apply the target palette. | ||
classList.add(palette); | ||
|
||
// Figure out palettes in need of removal. | ||
const otherPalettes = appliedPalettes.filter((paletteClass) => { | ||
return paletteClass !== palette; | ||
}); | ||
|
||
// Remove previously applied palette classes. | ||
otherPalettes.forEach((paletteClass) => { | ||
if (classList.contains(paletteClass)) { | ||
classList.remove(paletteClass); | ||
} | ||
}); | ||
}; |