Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to customize theme outside next.config.mjs #293

Open
mtr1990 opened this issue Oct 31, 2024 · 4 comments
Open

How to customize theme outside next.config.mjs #293

mtr1990 opened this issue Oct 31, 2024 · 4 comments
Assignees
Labels
customization: theme Centered around the theming features status: waiting for maintainer These issues haven't been looked at yet by a maintainer

Comments

@mtr1990
Copy link

mtr1990 commented Oct 31, 2024

Summary

In the default example the variables are set in next.config.mjs

How can we createTheme and import into next.config.mjs like

// /theme/palette.ts
export const palette = {
  primary: {
    light: '#C684FF',
    main: '#8E33FF',
    dark: '#5119B7',
    contrastText: '#FFFFFF',
  },
};

/theme/base-theme.ts
import { createTheme } from '@mui/material';
import { palette } from './palette';

export const baseTheme = createTheme({
  cssVariables: true,
  colorSchemes: {
    light: {
      palette,
    },
    dark: {
      palette,
    },
  },
  typography: {
    fontFamily: 'var(--font-roboto)',
  },
});

// and next.config.mjs
import { withPigment } from '@pigment-css/nextjs-plugin';
import {baseTheme} from './src/theme/base-theme';

/** @type {import('next').NextConfig} */
const nextConfig = {};

export default withPigment(nextConfig, {
  theme:baseTheme,
  transformLibraries: ['@mui/material'],
});

https://stackblitz.com/edit/github-xzdksv?file=next.config.mjs,src%2Fapp%2Fpage.tsx,src%2Ftheme%2Fbase-theme.mjs,src%2Ftheme%2Fpalette.ts

Examples

No response

Motivation

No response

Search keywords: Customize theme

@mtr1990 mtr1990 added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Oct 31, 2024
@zannager zannager added the customization: theme Centered around the theming features label Nov 1, 2024
@astrodomas
Copy link

astrodomas commented Nov 8, 2024

Another thing to note, people who are using a monorepo setup, for example - to store a design system as a separate library/package. They will have the theme styled there, thus making it impossible to import it to next.config.(js|ts|mjs) at all. Any suggestions from the maintainers on how to approach this?
Consider the following setup as using nrwl/nx monorepo as an example:

apps/next-app-1/next.config.js
libs/design-system/src/theme.js

If the import inside next.config.js will be made as @monorepo/design-system/theme, during build time you will get an error, meaning that theme.js cannot be reused.

@brijeshb42
Copy link
Contributor

If this about importing files that have not been transpiled yet into the next.config file ? I am not sure I fully understand the question.
In a monorepo setup, you can import theme from another package into your app package's next.config. If you aren't able to, I'd appreciate a repro in a github repo that I can debug.

@astrodomas
Copy link

If this about importing files that have not been transpiled yet into the next.config file ?

If this about importing files that have not been transpiled yet into the next.config file ? exactly

@brijeshb42
Copy link
Contributor

With latest version of next.js (v15), you can declare your config in next.config.ts and also import from ts files.
For older versions, you'll need a transpilation step in-between and we don't have a solution for that.
For older versions, it is really an issue with Next.js and we don't have much control over it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customization: theme Centered around the theming features status: waiting for maintainer These issues haven't been looked at yet by a maintainer
Projects
None yet
Development

No branches or pull requests

5 participants