0.3.3 • Published 2 years ago

antd-theme-vars v0.3.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Antd Theme Vars

Since Ant Design does not have a way to handle theme switch. This package will scan your theme folder and generate css and theme vars to override your less variables.

How it works:

Theme files

dark.less

@primary-color: #000000;

light.less

@primary-color: #AAAAAA;

Output theme.css:

.light {
  --primary-color: #AAAAAA;
}

.dark {
  --primary-color: #000000;
}

Output theme.vars:

{
  ['primary-color']: 'var(--primary-color);'
}

How to use

Create your theme files .less with only variables

  • src/themes/dark.less
  • src/themes/light.less

example: src/themes/dark.less

@primary-color: red; // primary color for all components
@gray-1: #ffffff;
@gray-2: #fafafa;
@gray-3: #f5f5f5;
@gray-4: #f0f0f0;
@gray-5: #d9d9d9;
@gray-6: #bfbfbf;
@gray-7: #8c8c8c;
@gray-8: #595959;
@gray-9: #434343;
@gray-10: #262626;

Create your theme.config.json on root folder

{
  "themesPath": "src/themes"
}

Update your next.config.js

const withLess = require('next-with-less')

// only server side
const { theme } = require('antd-theme-vars')

// ...

const nextConfig = {
  // ...
  publicRuntimeConfig: {
    theme: theme.vars // optionally only if you want to use available vars on client-side.
  },
  // ....
}

// ...

module.exports = withLess({
  ...nextConfig,
  lessLoaderOptions: {
    // it will add your themes by context, eg: .dark { --primary-color: red; } .light { --primary-color: yellow; }
    additionalData: theme.css,
    lessOptions: {
      // it will modify less vars, eg: { ['primary-color']: '--var(--primary-color)' }
      modifyVars: theme.vars,
      javascriptEnabled: true,
    }
  }
})

...

Create your theme switcher with Styled Components

example: src/themes/Theme.tsx

import getConfig from 'next/config';
import { ReactNode, useState } from 'react';
import { ThemeProvider as StyledThemeProvider } from 'styled-components';

export interface ThemeProviderProps {
  children: ReactNode
}

export function ThemeProvider({ children }: ThemeProviderProps) {
  const { publicRuntimeConfig } = getConfig()
  const [dark, setIsDark] = useState(false)

  return (
    <StyledThemeProvider theme={publicRuntimeConfig.theme || {}}>
      <div className={dark ? 'dark': 'light'}>
        <button onClick={() => setIsDark(!dark)}>Toggle theme</button>
        {children}
      </div>
    </StyledThemeProvider>
  )
}
0.3.3

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.9

2 years ago

0.2.8

2 years ago

0.2.7

2 years ago

0.2.6

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.13

2 years ago

0.1.12

2 years ago

0.1.11

2 years ago

0.1.10

2 years ago

0.1.9

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.3

2 years ago

0.1.1

2 years ago