There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.
TypeScript appcrdteditorelectronknowledge-basemarkdownmironotesnotionnotion-alternativerustrust-langrust-languagetabletableviewwhiteboardwikiworkspace
feat: add theme change handler
QiShaoXuan163f6cea08e8
diff --git a/src/components/simple-counter/index.ts b/src/components/simple-counter/index.tsindex 83b6dbde95..6f99529c1b 100644--- a/src/components/simple-counter/index.ts+++ b/src/components/simple-counter/index.ts@@ -1,7 +1,6 @@-import { LitElement, css, html, unsafeCSS } from 'lit';+import { LitElement, css, html } from 'lit';import { customElement, property, state } from 'lit/decorators.js';import * as React from 'react';-import { theme } from '@/styles';export const tagName = 'simple-counter';@@ -28,7 +27,7 @@ export class Counter extends LitElement {static styles = css`.counter-container {display: flex;- color: ${unsafeCSS(theme.colors.primary)};+ color: var(--color-primary);}button {margin: 0 5px;diff --git a/src/pages/_app.tsx b/src/pages/_app.tsxindex 9908d21a12..e118fcf54f 100644--- a/src/pages/_app.tsx+++ b/src/pages/_app.tsx@@ -1,12 +1,11 @@import type { AppProps } from 'next/app';-import { ThemeProvider } from '@emotion/react';-import { theme } from '../styles';+import { ThemeProvider } from '@/styles';import '../../public/globals.css';function MyApp({ Component, pageProps }: AppProps) {return (- <ThemeProvider theme={theme}>+ <ThemeProvider><Component {...pageProps} /></ThemeProvider>);diff --git a/src/pages/index.tsx b/src/pages/index.tsxindex ef4fe01580..66d8048e7e 100644--- a/src/pages/index.tsx+++ b/src/pages/index.tsx@@ -1,6 +1,5 @@import type { NextPage } from 'next';-import styled from '@emotion/styled';-+import { styled, useTheme } from '@/styles';import '@/components/simple-counter';const Button = styled('div')(({ theme }) => {@@ -10,10 +9,18 @@ const Button = styled('div')(({ theme }) => {});const Home: NextPage = () => {+ const { changeMode, mode } = useTheme();return (<div><Button>A button use the theme styles</Button><simple-counter name="A counter created by web component" />+ <button+ onClick={() => {+ changeMode(mode === 'dark' ? 'light' : 'dark');+ }}+ >+ current theme mode :{mode}(click to change)+ </button></div>);};diff --git a/src/styles/hooks.ts b/src/styles/hooks.tsnew file mode 100644index 0000000000..94398c7f36--- /dev/null+++ b/src/styles/hooks.ts@@ -0,0 +1,4 @@+import { useContext } from 'react';+import { ThemeContext } from './themeProvider';++export const useTheme = () => useContext(ThemeContext);diff --git a/src/styles/index.ts b/src/styles/index.tsindex 7b1f54ecf9..f1c02991ff 100644--- a/src/styles/index.ts+++ b/src/styles/index.ts@@ -1 +1,6 @@-export * from './theme';+export type { ThemeMode, ThemeProviderProps, AffineTheme } from './types';++export { styled } from './styled';+export { ThemeProvider } from './themeProvider';+export { lightTheme, darkTheme } from './theme';+export { useTheme } from './hooks';diff --git a/src/styles/styled.ts b/src/styles/styled.tsnew file mode 100644index 0000000000..11464cee29--- /dev/null+++ b/src/styles/styled.ts@@ -0,0 +1,3 @@+import emotionStyled from '@emotion/styled';++export const styled = emotionStyled;diff --git a/src/styles/theme.ts b/src/styles/theme.tsindex 45f6a84ed4..7afc298e8c 100644--- a/src/styles/theme.ts+++ b/src/styles/theme.ts@@ -1,17 +1,20 @@import '@emotion/react';+import { AffineTheme } from './types';-interface AffineTheme {+export const lightTheme: AffineTheme = {colors: {- primary: string;- };-}+ primary: '#0070f3',+ },+};-export const theme: AffineTheme = {+export const darkTheme: AffineTheme = {colors: {- primary: '#0070f3',+ primary: '#000',},};-declare module '@emotion/react' {- export interface Theme extends AffineTheme {}-}+export const globalThemeConstant = (theme: AffineTheme) => {+ return {+ '--color-primary': theme.colors.primary,+ };+};diff --git a/src/styles/themeProvider.tsx b/src/styles/themeProvider.tsxnew file mode 100644index 0000000000..383ae2dd74--- /dev/null+++ b/src/styles/themeProvider.tsx@@ -0,0 +1,39 @@+import {+ ThemeProvider as EmotionThemeProvider,+ Global,+ css,+} from '@emotion/react';+import { createContext, useState } from 'react';+import type { PropsWithChildren } from 'react';+import { ThemeMode, ThemeProviderProps, ThemeProviderValue } from './types';+import { lightTheme, darkTheme, globalThemeConstant } from './theme';++export const ThemeContext = createContext<ThemeProviderValue>({+ mode: 'light',+ changeMode: () => {},+ theme: lightTheme,+});++export const ThemeProvider = ({+ defaultMode = 'light',+ children,+}: PropsWithChildren<ThemeProviderProps>) => {+ const [mode, setMode] = useState<ThemeMode>(defaultMode);+ const theme = mode === 'dark' ? darkTheme : lightTheme;+ const changeMode = (themeMode: ThemeMode) => {+ setMode(themeMode);+ };++ return (+ <ThemeContext.Provider value={{ mode, changeMode, theme }}>+ <Global+ styles={css`+ :root {+ ${globalThemeConstant(theme)}+ }+ `}+ />+ <EmotionThemeProvider theme={theme}>{children}</EmotionThemeProvider>+ </ThemeContext.Provider>+ );+};diff --git a/src/styles/types.ts b/src/styles/types.tsnew file mode 100644index 0000000000..e3a9ce0476--- /dev/null+++ b/src/styles/types.ts@@ -0,0 +1,21 @@+export type ThemeMode = 'light' | 'dark';++export type ThemeProviderProps = {+ defaultMode?: ThemeMode;+};++export type ThemeProviderValue = {+ theme: AffineTheme;+ mode: ThemeMode;+ changeMode: (newMode: ThemeMode) => void;+};++export interface AffineTheme {+ colors: {+ primary: string;+ };+}++declare module '@emotion/react' {+ export interface Theme extends AffineTheme {}+}