Skip to content

Commit

Permalink
Add light suffix to the themeswitch light choices (#37)
Browse files Browse the repository at this point in the history
Co-authored-by: Snorre Nygaard <snorre.nygaard@finn.no>
  • Loading branch information
Skadefryd and Snorre Nygaard authored Oct 23, 2024
1 parent 981433e commit ee41d22
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions docs/.vitepress/warp-theme-switcher.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,50 @@
import { computed, reactive } from "vue";
import { computed, reactive } from 'vue';

export default {
install: (app) => {
const themes = {
'DBA': 'dba-dk',
'Finn': 'finn-no',
'Tori': 'tori-fi'
'DBA light': 'dba-dk',
'Finn light': 'finn-no',
'Tori light': 'tori-fi',
};

const state = reactive({
currentTheme: (typeof window !== 'undefined' && localStorage.getItem('warpTheme')) || themes.Finn
currentTheme:
(typeof window !== 'undefined' &&
localStorage.getItem('warpTheme')) ||
themes.Finn,
});

const rewriteStylesheets = (theme) => {
const roots = [document, ...Array.from(document.querySelectorAll('*')).filter((el) => !!el.shadowRoot).map((el) => el.shadowRoot)];
const roots = [
document,
...Array.from(document.querySelectorAll('*'))
.filter((el) => !!el.shadowRoot)
.map((el) => el.shadowRoot),
];
roots.forEach((root) => {
const stylesheets = root.querySelectorAll('link[rel="stylesheet"][href*="/@warp-ds/css/"]');
const stylesheets = root.querySelectorAll(
'link[rel="stylesheet"][href*="/@warp-ds/css/"]'
);
stylesheets.forEach((s) => {
s.setAttribute(
'href',
`https://assets.finn.no/pkg/@warp-ds/css/v2/tokens/${theme}.css`,
`https://assets.finn.no/pkg/@warp-ds/css/v2/tokens/${theme}.css`
);
});
});
};

const updateTheme = (theme) => {
localStorage.setItem('warpTheme', theme);
state.currentTheme = theme; // Update the reactive state
state.currentTheme = theme; // Update the reactive state
rewriteStylesheets(theme);
};

app.provide('warpThemeSwitcher', {
themes,
current: computed(() => state.currentTheme),
updateTheme
updateTheme,
});

// Listen for storage changes
Expand All @@ -46,5 +56,5 @@ export default {
}
});
}
}
}
},
};

0 comments on commit ee41d22

Please sign in to comment.