Skip to content

Commit

Permalink
feat: load dark/light theme for skin
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Jan 13, 2025
1 parent f56edab commit ea49e08
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion apps/ui/src/components/App/Topnav.vue
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ onUnmounted(() => {
</template>
</UiButton>
<IndicatorPendingTransactions />
<UiButton class="!px-0 w-[46px]" @click="toggleSkin">
<UiButton class="!px-0 w-[46px]" @click="toggleSkin()">
<IH-light-bulb v-if="currentMode === 'dark'" class="inline-block" />
<IH-moon v-else class="inline-block" />
</UiButton>
Expand Down
12 changes: 9 additions & 3 deletions apps/ui/src/components/Layout/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const router = useRouter();
const uiStore = useUiStore();
const { modalOpen } = useModal();
const { init, setAppName, app } = useApp();
const { DEFAULT_SKIN, toggleSkin } = useUserSkin();
const { isWhiteLabel, space: whiteLabelSpace } = useWhiteLabel();
const { setFavicon } = useFavicon();
const { web3 } = useWeb3();
Expand Down Expand Up @@ -70,13 +71,13 @@ const hasTopNav = computed(() => {
return 'space-editor' !== String(route.matched[1]?.name);
});
const skinSettings = computed(() => {
const skinVariables = computed(() => {
if (!whiteLabelSpace.value?.additionalRawData?.skinSettings) return {};
const colors = clone(whiteLabelSpace.value?.additionalRawData?.skinSettings);
const result = Object.entries(colors).reduce((acc, [colorName, hex]) => {
if (!hex) return acc;
if (!hex || !colorName.includes('_color')) return acc;
const rgb = hexToRgb(hex.slice(1));
acc[`--${colorName.replace('_color', '')}`] = `${rgb.r},${rgb.g},${rgb.b}`;
Expand Down Expand Up @@ -168,9 +169,14 @@ watch(
setAppName(whiteLabelSpace.value.name);
css.value = `:root { ${Object.entries(skinSettings.value)
css.value = `:root { ${Object.entries(skinVariables.value)
.map(([key, val]) => `${key}:${val}`)
.join(';')}; }`;
toggleSkin(
whiteLabelSpace.value.additionalRawData?.skinSettings?.theme ||
DEFAULT_SKIN
);
},
{ immediate: true }
);
Expand Down
13 changes: 9 additions & 4 deletions apps/ui/src/composables/useUserSkin.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
type Skin = 'dark' | 'light' | 'none';

const DEFAULT_SKIN: Skin = 'light';

export function useUserSkin() {
const store = useStorage<Skin>('skin', 'none');
const currentMode = computed(() =>
['light', 'none'].includes(store.value) ? 'light' : 'dark'
[DEFAULT_SKIN, 'none'].includes(store.value) ? DEFAULT_SKIN : 'dark'
);

function toggleSkin() {
store.value = ['light', 'none'].includes(store.value) ? 'dark' : 'light';
function toggleSkin(skin?: Skin) {
store.value =
skin ||
([DEFAULT_SKIN, 'none'].includes(store.value) ? 'dark' : DEFAULT_SKIN);
}

watchEffect(() => {
if (currentMode.value === 'light') {
if (currentMode.value === DEFAULT_SKIN) {
document.documentElement.classList.remove('dark');
} else {
document.documentElement.classList.add('dark');
}
});

return {
DEFAULT_SKIN,
currentMode,
toggleSkin
};
Expand Down
1 change: 1 addition & 0 deletions apps/ui/src/networks/offchain/api/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ const SPACE_FRAGMENT = gql`
border_color
heading_color
primary_color
theme
}
guidelines
template
Expand Down
2 changes: 1 addition & 1 deletion apps/ui/src/networks/offchain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { createApi } from './api';
import * as constants from './constants';

const HUB_URLS: Partial<Record<NetworkID, string | undefined>> = {
s: 'https://hub.snapshot.org/graphql',
s: 'http://localhost:3000/graphql',
's-tn': 'https://testnet.hub.snapshot.org/graphql'
};
export const SNAPSHOT_URLS: Partial<Record<NetworkID, string | undefined>> = {
Expand Down
1 change: 1 addition & 0 deletions apps/ui/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ export type SkinSettings = {
border_color?: string;
heading_color?: string;
primary_color?: string;
theme?: string;
};

export type Drafts = Record<string, Draft>;
Expand Down

0 comments on commit ea49e08

Please sign in to comment.