mirror of
https://github.com/ajnart/homarr.git
synced 2025-11-10 07:25:48 +01:00
🎨 Made color switcher change Mantine styles
Moved the color switcher's functions to a context provider and made Mantine's styles derived off of that context.
This commit is contained in:
@@ -39,12 +39,7 @@ export function ColorSchemeSwitch() {
|
|||||||
<div className={classes.root}>
|
<div className={classes.root}>
|
||||||
<Sun className={cx(classes.icon, classes.iconLight)} size={18} />
|
<Sun className={cx(classes.icon, classes.iconLight)} size={18} />
|
||||||
<MoonStars className={cx(classes.icon, classes.iconDark)} size={18} />
|
<MoonStars className={cx(classes.icon, classes.iconDark)} size={18} />
|
||||||
<Switch
|
<Switch checked={colorScheme === 'dark'} onChange={() => toggleColorScheme()} size="md" />
|
||||||
color={config.settings.primaryColor || 'red'}
|
|
||||||
checked={colorScheme === 'dark'}
|
|
||||||
onChange={() => toggleColorScheme()}
|
|
||||||
size="md"
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
Switch to {colorScheme === 'dark' ? 'light' : 'dark'} mode
|
Switch to {colorScheme === 'dark' ? 'light' : 'dark'} mode
|
||||||
<Group spacing={2}>
|
<Group spacing={2}>
|
||||||
|
|||||||
@@ -59,20 +59,13 @@ export default function SaveConfigComponent(props: any) {
|
|||||||
</Group>
|
</Group>
|
||||||
</form>
|
</form>
|
||||||
</Modal>
|
</Modal>
|
||||||
<Button
|
<Button size="xs" leftIcon={<Download />} variant="outline" onClick={onClick}>
|
||||||
size="xs"
|
|
||||||
leftIcon={<Download />}
|
|
||||||
variant="outline"
|
|
||||||
color={config.settings.primaryColor || 'red'}
|
|
||||||
onClick={onClick}
|
|
||||||
>
|
|
||||||
Download config
|
Download config
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
size="xs"
|
size="xs"
|
||||||
leftIcon={<Trash />}
|
leftIcon={<Trash />}
|
||||||
variant="outline"
|
variant="outline"
|
||||||
color={config.settings.primaryColor || 'red'}
|
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
axios
|
axios
|
||||||
.delete(`/api/configs/${config.name}`)
|
.delete(`/api/configs/${config.name}`)
|
||||||
@@ -101,13 +94,7 @@ export default function SaveConfigComponent(props: any) {
|
|||||||
>
|
>
|
||||||
Delete config
|
Delete config
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button size="xs" leftIcon={<Plus />} variant="outline" onClick={() => setOpened(true)}>
|
||||||
size="xs"
|
|
||||||
leftIcon={<Plus />}
|
|
||||||
variant="outline"
|
|
||||||
color={config.settings.primaryColor || 'red'}
|
|
||||||
onClick={() => setOpened(true)}
|
|
||||||
>
|
|
||||||
Save a copy
|
Save a copy
|
||||||
</Button>
|
</Button>
|
||||||
</Group>
|
</Group>
|
||||||
|
|||||||
@@ -44,9 +44,7 @@ export default function TitleChanger() {
|
|||||||
{...form.getInputProps('favicon')}
|
{...form.getInputProps('favicon')}
|
||||||
/>
|
/>
|
||||||
<TextInput label="Background" placeholder="" {...form.getInputProps('background')} />
|
<TextInput label="Background" placeholder="" {...form.getInputProps('background')} />
|
||||||
<Button type="submit" color={config.settings.primaryColor || 'red'}>
|
<Button type="submit">Save</Button>
|
||||||
Save
|
|
||||||
</Button>
|
|
||||||
</Group>
|
</Group>
|
||||||
</form>
|
</form>
|
||||||
</Group>
|
</Group>
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
import { ColorSwatch, Group, Popover, Text, useMantineTheme } from '@mantine/core';
|
import { ColorSwatch, Group, Popover, Text, useMantineTheme } from '@mantine/core';
|
||||||
import { useConfig } from '../../tools/state';
|
import { useConfig } from '../../tools/state';
|
||||||
|
import { useColorTheme } from '../../tools/color';
|
||||||
|
|
||||||
interface ColorControlProps {
|
interface ColorControlProps {
|
||||||
type: string;
|
type: string;
|
||||||
@@ -9,19 +10,20 @@ interface ColorControlProps {
|
|||||||
export function ColorSelector({ type }: ColorControlProps) {
|
export function ColorSelector({ type }: ColorControlProps) {
|
||||||
const { config, setConfig } = useConfig();
|
const { config, setConfig } = useConfig();
|
||||||
const [opened, setOpened] = useState(false);
|
const [opened, setOpened] = useState(false);
|
||||||
|
|
||||||
|
const { primaryColor, secondaryColor, setPrimaryColor, setSecondaryColor } = useColorTheme();
|
||||||
|
|
||||||
const theme = useMantineTheme();
|
const theme = useMantineTheme();
|
||||||
const colors = Object.keys(theme.colors).map((color) => ({
|
const colors = Object.keys(theme.colors).map((color) => ({
|
||||||
swatch: theme.colors[color][6],
|
swatch: theme.colors[color][6],
|
||||||
color,
|
color,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const configColor =
|
const configColor = type === 'primary' ? primaryColor : secondaryColor;
|
||||||
type === 'primary'
|
|
||||||
? config.settings.primaryColor || 'red'
|
|
||||||
: config.settings.secondaryColor || 'orange';
|
|
||||||
|
|
||||||
const setConfigColor = (color: string) => {
|
const setConfigColor = (color: string) => {
|
||||||
if (type === 'primary') {
|
if (type === 'primary') {
|
||||||
|
setPrimaryColor(color);
|
||||||
setConfig({
|
setConfig({
|
||||||
...config,
|
...config,
|
||||||
settings: {
|
settings: {
|
||||||
@@ -30,6 +32,7 @@ export function ColorSelector({ type }: ColorControlProps) {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
setSecondaryColor(color);
|
||||||
setConfig({
|
setConfig({
|
||||||
...config,
|
...config,
|
||||||
settings: {
|
settings: {
|
||||||
@@ -62,7 +65,7 @@ export function ColorSelector({ type }: ColorControlProps) {
|
|||||||
<ColorSwatch
|
<ColorSwatch
|
||||||
component="button"
|
component="button"
|
||||||
type="button"
|
type="button"
|
||||||
color={theme.colors[configColor || 'red'][6]}
|
color={theme.colors[configColor][6]}
|
||||||
onClick={() => setOpened((o) => !o)}
|
onClick={() => setOpened((o) => !o)}
|
||||||
size={22}
|
size={22}
|
||||||
style={{ display: 'block', cursor: 'pointer' }}
|
style={{ display: 'block', cursor: 'pointer' }}
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ export default function ModuleEnabler(props: any) {
|
|||||||
size="md"
|
size="md"
|
||||||
checked={config.modules?.[module.title]?.enabled ?? false}
|
checked={config.modules?.[module.title]?.enabled ?? false}
|
||||||
label={`Enable ${module.title}`}
|
label={`Enable ${module.title}`}
|
||||||
color={config.settings.primaryColor || 'red'}
|
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
setConfig({
|
setConfig({
|
||||||
...config,
|
...config,
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
import { Group, Image, Text } from '@mantine/core';
|
import { Group, Image, Text } from '@mantine/core';
|
||||||
import { NextLink } from '@mantine/next';
|
import { NextLink } from '@mantine/next';
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
|
import { useColorTheme } from '../../tools/color';
|
||||||
import { useConfig } from '../../tools/state';
|
import { useConfig } from '../../tools/state';
|
||||||
|
|
||||||
export function Logo({ style }: any) {
|
export function Logo({ style }: any) {
|
||||||
const { config } = useConfig();
|
const { config } = useConfig();
|
||||||
|
const { primaryColor, secondaryColor } = useColorTheme();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Group spacing="xs">
|
<Group spacing="xs">
|
||||||
@@ -27,8 +29,8 @@ export function Logo({ style }: any) {
|
|||||||
weight="bold"
|
weight="bold"
|
||||||
variant="gradient"
|
variant="gradient"
|
||||||
gradient={{
|
gradient={{
|
||||||
from: config.settings.primaryColor || 'red',
|
from: primaryColor,
|
||||||
to: config.settings.secondaryColor || 'orange',
|
to: secondaryColor,
|
||||||
deg: 145,
|
deg: 145,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -6,15 +6,24 @@ import Head from 'next/head';
|
|||||||
import { MantineProvider, ColorScheme, ColorSchemeProvider } from '@mantine/core';
|
import { MantineProvider, ColorScheme, ColorSchemeProvider } from '@mantine/core';
|
||||||
import { NotificationsProvider } from '@mantine/notifications';
|
import { NotificationsProvider } from '@mantine/notifications';
|
||||||
import { useHotkeys } from '@mantine/hooks';
|
import { useHotkeys } from '@mantine/hooks';
|
||||||
import { ConfigProvider, useConfig } from '../tools/state';
|
import { ConfigProvider } from '../tools/state';
|
||||||
import { theme } from '../tools/theme';
|
import { theme } from '../tools/theme';
|
||||||
import { styles } from '../tools/styles';
|
import { styles } from '../tools/styles';
|
||||||
|
import { ColorTheme } from '../tools/color';
|
||||||
|
|
||||||
export default function App(props: AppProps & { colorScheme: ColorScheme }) {
|
export default function App(this: any, props: AppProps & { colorScheme: ColorScheme }) {
|
||||||
const { Component, pageProps } = props;
|
const { Component, pageProps } = props;
|
||||||
const { config } = useConfig();
|
|
||||||
const [colorScheme, setColorScheme] = useState<ColorScheme>(props.colorScheme);
|
const [colorScheme, setColorScheme] = useState<ColorScheme>(props.colorScheme);
|
||||||
|
|
||||||
|
const [primaryColor, setPrimaryColor] = useState<string>('red');
|
||||||
|
const [secondaryColor, setSecondaryColor] = useState<string>('orange');
|
||||||
|
const colorTheme = {
|
||||||
|
primaryColor,
|
||||||
|
secondaryColor,
|
||||||
|
setPrimaryColor,
|
||||||
|
setSecondaryColor,
|
||||||
|
};
|
||||||
|
|
||||||
const toggleColorScheme = (value?: ColorScheme) => {
|
const toggleColorScheme = (value?: ColorScheme) => {
|
||||||
const nextColorScheme = value || (colorScheme === 'dark' ? 'light' : 'dark');
|
const nextColorScheme = value || (colorScheme === 'dark' ? 'light' : 'dark');
|
||||||
setColorScheme(nextColorScheme);
|
setColorScheme(nextColorScheme);
|
||||||
@@ -31,10 +40,11 @@ export default function App(props: AppProps & { colorScheme: ColorScheme }) {
|
|||||||
</Head>
|
</Head>
|
||||||
|
|
||||||
<ColorSchemeProvider colorScheme={colorScheme} toggleColorScheme={toggleColorScheme}>
|
<ColorSchemeProvider colorScheme={colorScheme} toggleColorScheme={toggleColorScheme}>
|
||||||
|
<ColorTheme.Provider value={colorTheme}>
|
||||||
<MantineProvider
|
<MantineProvider
|
||||||
theme={{
|
theme={{
|
||||||
...theme,
|
...theme,
|
||||||
primaryColor: config.settings.primaryColor || 'red',
|
primaryColor,
|
||||||
colorScheme,
|
colorScheme,
|
||||||
}}
|
}}
|
||||||
styles={{
|
styles={{
|
||||||
@@ -49,6 +59,7 @@ export default function App(props: AppProps & { colorScheme: ColorScheme }) {
|
|||||||
</ConfigProvider>
|
</ConfigProvider>
|
||||||
</NotificationsProvider>
|
</NotificationsProvider>
|
||||||
</MantineProvider>
|
</MantineProvider>
|
||||||
|
</ColorTheme.Provider>
|
||||||
</ColorSchemeProvider>
|
</ColorSchemeProvider>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import { Config } from '../tools/types';
|
|||||||
import { useConfig } from '../tools/state';
|
import { useConfig } from '../tools/state';
|
||||||
import { migrateToIdConfig } from '../tools/migrate';
|
import { migrateToIdConfig } from '../tools/migrate';
|
||||||
import { getConfig } from '../tools/getConfig';
|
import { getConfig } from '../tools/getConfig';
|
||||||
|
import { useColorTheme } from '../tools/color';
|
||||||
import Layout from '../components/layout/Layout';
|
import Layout from '../components/layout/Layout';
|
||||||
|
|
||||||
export async function getServerSideProps({
|
export async function getServerSideProps({
|
||||||
@@ -29,8 +30,11 @@ export async function getServerSideProps({
|
|||||||
export default function HomePage(props: any) {
|
export default function HomePage(props: any) {
|
||||||
const { config: initialConfig }: { config: Config } = props;
|
const { config: initialConfig }: { config: Config } = props;
|
||||||
const { setConfig } = useConfig();
|
const { setConfig } = useConfig();
|
||||||
|
const { setPrimaryColor, setSecondaryColor } = useColorTheme();
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const migratedConfig = migrateToIdConfig(initialConfig);
|
const migratedConfig = migrateToIdConfig(initialConfig);
|
||||||
|
setPrimaryColor(migratedConfig.settings.primaryColor || 'red');
|
||||||
|
setSecondaryColor(migratedConfig.settings.secondaryColor || 'orange');
|
||||||
setConfig(migratedConfig);
|
setConfig(migratedConfig);
|
||||||
}, [initialConfig]);
|
}, [initialConfig]);
|
||||||
return (
|
return (
|
||||||
|
|||||||
23
src/tools/color.ts
Normal file
23
src/tools/color.ts
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import { createContext, useContext } from 'react';
|
||||||
|
|
||||||
|
type colorThemeContextType = {
|
||||||
|
primaryColor: string;
|
||||||
|
secondaryColor: string;
|
||||||
|
setPrimaryColor: (color: string) => void;
|
||||||
|
setSecondaryColor: (color: string) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const ColorTheme = createContext<colorThemeContextType>({
|
||||||
|
primaryColor: 'red',
|
||||||
|
secondaryColor: 'orange',
|
||||||
|
setPrimaryColor: () => {},
|
||||||
|
setSecondaryColor: () => {},
|
||||||
|
});
|
||||||
|
|
||||||
|
export function useColorTheme() {
|
||||||
|
const context = useContext(ColorTheme);
|
||||||
|
if (context === undefined) {
|
||||||
|
throw new Error('useColorTheme must be used within a ColorTheme.Provider');
|
||||||
|
}
|
||||||
|
return context;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user