Skip to content

Commit

Permalink
intorduce theme types - refactor as a service object
Browse files Browse the repository at this point in the history
  • Loading branch information
edeleastar committed Dec 5, 2024
1 parent 51f49f3 commit 07f667f
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 89 deletions.
4 changes: 2 additions & 2 deletions src/lib/services/models/course-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addIcon } from "$lib/ui/themes/theme-controller.svelte";
import { themeService } from "$lib/ui/themes/theme-controller.svelte";
import type { Composite, Course, IconNav, Lo, LoType, Topic } from "./lo-types";
import { filterByType, setShowHide } from "./lo-utils";

Expand Down Expand Up @@ -51,7 +51,7 @@ export function createCompanions(course: Course) {
if (course.properties.companions) {
for (const [key, value] of Object.entries(course.properties.companions)) {
const companion: any = value;
addIcon(key, companion.icon);
themeService.addIcon(key, companion.icon);
course.companions.bar.push({
link: companion.link,
type: key,
Expand Down
7 changes: 7 additions & 0 deletions src/lib/services/models/lo-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ export type IconNavBar = {
bar: IconNav[];
};

export type IconLib = Record<string, IconType>;

export type Theme = {
name: string;
icons: IconLib;
};

export interface LearningRecord {
date: Date;
pageLoads: number;
Expand Down
6 changes: 3 additions & 3 deletions src/lib/ui/components/Card.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
textSize
} from "$lib/runes";
import { getTypeColour } from "../themes/theme-controller.svelte";
import type { CardDetails } from "$lib/services/types.svelte";
import Icon from "$lib/ui/components/Icon.svelte";
import { themeService } from "../themes/theme-controller.svelte";
let { cardDetails } = $props<{ cardDetails: CardDetails }>();
let target = $state("");
Expand Down Expand Up @@ -104,8 +104,8 @@

<a href={cardDetails.route} {target}>
<div
class="card preset-filled-{getTypeColour(cardDetails.type)}-100-900 border-[1px]
border-y-8 border-{getTypeColour(
class="card preset-filled-{themeService.getTypeColour(cardDetails.type)}-100-900 border-[1px]
border-y-8 border-{themeService.getTypeColour(
cardDetails.type
)}-500 m-2 {cardWidths.value} transition-all hover:scale-105 {cardHeight.value}
flex flex-col"
Expand Down
16 changes: 13 additions & 3 deletions src/lib/ui/components/Icon.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import Icon from "@iconify/svelte";
import { Tooltip } from "@skeletonlabs/skeleton-svelte";
import { getIcon } from "../themes/theme-controller.svelte";
import { themeService } from "../themes/theme-controller.svelte";
interface Props {
type?: string;
Expand Down Expand Up @@ -51,11 +51,21 @@
{#if type}
{#if link}
<a class="btn btn-sm" {target} href={link}>
<Icon icon={getIcon(type).type} color={legacyIconColour(getIcon(type).color)} {width} {height} />
<Icon
icon={themeService.getIcon(type).type}
color={legacyIconColour(themeService.getIcon(type).color)}
{width}
{height}
/>
{text}
</a>
{:else}
<Icon icon={getIcon(type).type} color={legacyIconColour(getIcon(type).color)} {width} {height} />
<Icon
icon={themeService.getIcon(type).type}
color={legacyIconColour(themeService.getIcon(type).color)}
{width}
{height}
/>
{/if}
{:else if link}
<a {target} href={link}>
Expand Down
4 changes: 2 additions & 2 deletions src/lib/ui/learning-objects/content/Video.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { onMount } from "svelte";
import type { Lo } from "$lib/services/models/lo-types";
import { currentCourse } from "$lib/runes";
import { getIcon } from "../../themes/theme-controller.svelte";
import { themeService } from "$lib/ui/themes/theme-controller.svelte";
let firefox = $state(false);
Expand Down Expand Up @@ -33,7 +33,7 @@
});
if (lo && lo.type === "panelvideo") {
lo.icon = { type: getIcon("video").type, color: getIcon("video").color };
lo.icon = { type: themeService.getIcon("video").type, color: themeService.getIcon("video").color };
}
if (lo.videoids) {
Expand Down
10 changes: 5 additions & 5 deletions src/lib/ui/themes/LayoutMenu.svelte
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<script lang="ts">
import { setDisplayMode, setTheme, themes } from "./theme-controller.svelte";
import { currentTheme, lightMode } from "$lib/runes";
import Menu from "$lib/ui/components/Menu.svelte";
import { layout } from "$lib/runes";
import MenuItem from "$lib/ui/components/MenuItem.svelte";
import Icon from "../components/Icon.svelte";
import { themeService } from "./theme-controller.svelte";
function changeTheme(theme: string): void {
setTheme(theme);
themeService.setTheme(theme);
}
function toggleDisplayMode(): void {
if (lightMode.value === "dark") {
setDisplayMode("light");
themeService.setDisplayMode("light");
} else {
setDisplayMode("dark");
themeService.setDisplayMode("dark");
}
}
Expand Down Expand Up @@ -44,7 +44,7 @@
<hr />
<h6>Themes</h6>
<ul class="list">
{#each themes as theme}
{#each themeService.themes as theme}
<MenuItem
type="lightMode"
isActive={currentTheme.value === theme.name}
Expand Down
4 changes: 3 additions & 1 deletion src/lib/ui/themes/icons/festive-icons.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const FestiveIcons = {
import type { IconLib } from "$lib/services/models/lo-types";

export const FestiveIcons: IconLib = {
// Home Icon
programHome: { type: "tabler:christmas-ball", color: "success" },

Expand Down
4 changes: 3 additions & 1 deletion src/lib/ui/themes/icons/fluent-icons.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const FluentIconLib = {
import type { IconLib } from "$lib/services/models/lo-types";

export const FluentIconLib: IconLib = {
// Home Icon
programHome: { type: "fluent:home-24-filled", color: "success" },

Expand Down
4 changes: 3 additions & 1 deletion src/lib/ui/themes/icons/halloween-icons.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const HalloweenIconLib = {
import type { IconLib } from "$lib/services/models/lo-types";

export const HalloweenIconLib: IconLib = {
// Home type
programHome: { type: "cib:nodemon", color: "warning" },

Expand Down
4 changes: 3 additions & 1 deletion src/lib/ui/themes/icons/hero-icons.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const HeroIconLib = {
import type { IconLib } from "$lib/services/models/lo-types";

export const HeroIconLib: IconLib = {
// Home Icon
programHome: { type: "heroicons-outline:home", color: "success" },

Expand Down
141 changes: 76 additions & 65 deletions src/lib/ui/themes/theme-controller.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,90 @@
import { currentTheme, lightMode } from "$lib/runes";
import type { IconType } from "$lib/services/models/lo-types";
import type { IconType, Theme } from "$lib/services/models/lo-types";
import { FluentIconLib } from "./icons/fluent-icons";
import { HeroIconLib } from "./icons/hero-icons";
import { FestiveIcons } from "./icons/festive-icons";
import { makeItSnow, makeItStopSnowing } from "./events/festive.svelte";

export const themes = [
{ name: "tutors", icons: FluentIconLib },
{ name: "classic", icons: FluentIconLib },
{ name: "dyslexia", icons: FluentIconLib },
{ name: "festive", icons: FestiveIcons },
{ name: "nouveau", icons: FluentIconLib },
{ name: "rose", icons: FluentIconLib },
{ name: "cerberus", icons: FluentIconLib }
];
export const themeService = {
themes: [
{ name: "tutors", icons: FluentIconLib },
{ name: "classic", icons: FluentIconLib },
{ name: "dyslexia", icons: FluentIconLib },
{ name: "festive", icons: FestiveIcons },
{ name: "nouveau", icons: FluentIconLib },
{ name: "rose", icons: FluentIconLib },
{ name: "cerberus", icons: FluentIconLib }
] as Theme[],

export function initDisplay(): void {
if (localStorage.modeCurrent) {
lightMode.value = localStorage.modeCurrent;
} else {
lightMode.value = "light";
}
setDisplayMode(localStorage.modeCurrent);
setTheme(localStorage.theme);
}
isSnowing: false,

export function setDisplayMode(mode: string): void {
lightMode.value = mode;
localStorage.modeCurrent = mode;
if (mode === "dark") {
document.body.classList.add("dark");
} else {
document.body.classList.remove("dark");
}
}
initDisplay(): void {
if (localStorage.modeCurrent) {
lightMode.value = localStorage.modeCurrent;
} else {
lightMode.value = "light";
}
this.setDisplayMode(localStorage.modeCurrent);
this.setTheme(localStorage.theme);
},

export function setTheme(theme: string): void {
if (!theme) {
theme = "tutors";
}
if (themes.find((theme) => theme.name === currentTheme.value)) {
currentTheme.value = theme;
} else {
currentTheme.value = "tutors";
}
document.body.setAttribute("data-theme", currentTheme.value);
localStorage.theme = currentTheme.value;
setDisplayMode(mode: string): void {
lightMode.value = mode;
localStorage.modeCurrent = mode;
if (mode === "dark") {
document.body.classList.add("dark");
} else {
document.body.classList.remove("dark");
}
},

if (currentTheme.value === "festive") {
makeItSnow();
} else {
makeItStopSnowing();
}
}
setTheme(theme: string): void {
if (!theme) {
theme = "tutors";
}
if (themeService.themes.find((theme) => theme.name === currentTheme.value)) {
currentTheme.value = theme;
} else {
currentTheme.value = "tutors";
}
document.body.setAttribute("data-theme", currentTheme.value);
localStorage.theme = currentTheme.value;
this.eventTrigger();
},

export function getIcon(type: string): IconType {
const iconLib = themes.find((theme) => theme.name === currentTheme.value)?.icons;
if (iconLib && iconLib[type]) {
return iconLib[type];
} else {
console.log("No type found for icon", type);
return FluentIconLib.tutors;
}
}
getIcon(type: string): IconType {
const iconLib = themeService.themes.find((theme) => theme.name === currentTheme.value)?.icons;
if (iconLib && iconLib[type]) {
return iconLib[type];
} else {
console.log("No type found for icon", type);
return FluentIconLib.tutors;
}
},

addIcon(type: string, icon: IconType) {
FluentIconLib[type] = icon;
HeroIconLib[type] = icon;
FestiveIcons[type] = icon;
},

export function addIcon(type: string, icon: IconType) {
FluentIconLib[type] = icon;
HeroIconLib[type] = icon;
FestiveIcons[type] = icon;
}
getTypeColour(type: string): string {
const iconLib = themeService.themes.find((theme) => theme.name === currentTheme.value)?.icons;
if (iconLib && iconLib[type]) {
return iconLib[type].color;
}
return "primary";
},

export function getTypeColour(type: string): string {
const iconLib = themes.find((theme) => theme.name === currentTheme.value)?.icons;
if (iconLib && iconLib[type]) {
return iconLib[type].color;
eventTrigger(): void {
if (currentTheme.value === "festive") {
makeItSnow();
this.isSnowing = true;
} else {
if (this.isSnowing) {
this.isSnowing = false;
makeItStopSnowing();
}
}
}
return "primary";
}
};
12 changes: 9 additions & 3 deletions src/routes/(auth)/auth/SigninWithGithub.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<script lang="ts">
import { getIcon } from "$lib/ui/themes/theme-controller.svelte";
import Icon from "@iconify/svelte";
import { Progress } from "@skeletonlabs/skeleton-svelte";
import { tutorsConnectService } from "$lib/services/connect.svelte";
import TutorsTerms from "./TutorsTerms.svelte";
import { themeService } from "$lib/ui/themes/theme-controller.svelte";
let showProgress = $state(false);
interface Props {
Expand All @@ -20,7 +20,10 @@

<div class="bg-surface-100-800 mx-auto mb-2 place-items-center overflow-hidden rounded-xl p-4">
<div class="flex flex-wrap justify-center">
<div class="card w-4/5 border-y-8 !bg-surface-50 dark:!bg-surface-700 border-{getIcon('note').color}-500 m-2">
<div
class="card w-4/5 border-y-8 !bg-surface-50 dark:!bg-surface-700 border-{themeService.getIcon('note')
.color}-500 m-2"
>
<header class="card-header flex flex-row items-center justify-between p-3">
<div class="flex-auto text-center !text-black dark:!text-white">Tutors Sign In</div>
</header>
Expand All @@ -45,7 +48,10 @@
{/if}
</footer>
</div>
<div class="card w-4/5 border-y-8 !bg-surface-50 dark:!bg-surface-700 border-{getIcon('topic').color}-500 m-2">
<div
class="card w-4/5 border-y-8 !bg-surface-50 dark:!bg-surface-700 border-{themeService.getIcon('topic')
.color}-500 m-2"
>
<footer class="card-footer mt-4">
<article class="prose mx-auto w-[80%] max-w-none dark:prose-invert">
<TutorsTerms />
Expand Down
4 changes: 2 additions & 2 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import type { PageData } from "./$types";
import { onMount } from "svelte";
import { browser } from "$app/environment";
import { initDisplay, setTheme } from "$lib/ui/themes/theme-controller.svelte";
import { themeService } from "$lib/ui/themes/theme-controller.svelte";
interface Props {
data: PageData;
Expand All @@ -18,7 +18,7 @@
onMount(async () => {
if (browser) {
initDisplay();
themeService.initDisplay();
}
});
</script>
Expand Down

0 comments on commit 07f667f

Please sign in to comment.