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

[examples] Add pigment-css-nextjs-ts starter project #41105

Merged
merged 27 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
b9dd1ff
init zero-nextjs example
siriwatknp Feb 7, 2024
3e34f13
Merge branch 'master' of https://github.com/mui/material-ui into zero…
siriwatknp Feb 7, 2024
6bec039
Merge branch 'master' of https://github.com/mui/material-ui into zero…
siriwatknp Feb 13, 2024
de7ff2e
Merge branch 'master' of https://github.com/mui/material-ui into zero…
siriwatknp Feb 14, 2024
839a284
add keyframes to README
siriwatknp Feb 15, 2024
69ebd43
add zero-nextjs starter
siriwatknp Feb 15, 2024
c294dfb
update project README
siriwatknp Feb 15, 2024
a8e8f09
add styles import to the docs
siriwatknp Feb 15, 2024
272e1bc
Merge branch 'master' of https://github.com/mui/material-ui into zero…
siriwatknp Feb 19, 2024
539e5c3
convert to object styles
siriwatknp Feb 19, 2024
3a1e93e
move global css to zero-runtime
siriwatknp Feb 19, 2024
85087cc
test server action
siriwatknp Feb 19, 2024
cd8ddd2
Revert "test server action"
siriwatknp Feb 20, 2024
47e1c1a
add comment
siriwatknp Feb 20, 2024
e3f4ba0
update tsconfig
siriwatknp Feb 20, 2024
75c67f3
update version to 'latest'
siriwatknp Feb 20, 2024
3013638
add `applyStyles` to README
siriwatknp Feb 20, 2024
7585b0e
Apply suggestions from code review
siriwatknp Feb 26, 2024
ad28f60
Merge branch 'master' of https://github.com/mui/material-ui into zero…
siriwatknp Feb 26, 2024
3bba2e1
run prettier
siriwatknp Feb 26, 2024
c88f7e9
Merge branch 'master' of https://github.com/mui/material-ui into zero…
siriwatknp Feb 27, 2024
cf7d148
add types comment
siriwatknp Feb 27, 2024
cea6412
Merge branch 'master' of https://github.com/mui/material-ui into zero…
siriwatknp Mar 4, 2024
4114c83
replace zero with pigment
siriwatknp Mar 4, 2024
178a00a
use pigment-css
siriwatknp Mar 4, 2024
8288b23
Merge branch 'master' of https://github.com/mui/material-ui into zero…
siriwatknp Mar 8, 2024
bcd0182
update readme
siriwatknp Mar 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions examples/zero-nextjs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
# next-env.d.ts
38 changes: 38 additions & 0 deletions examples/zero-nextjs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Zero Runtime - Next.js App Router example in TypeScript
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved

This is a [Next.js](https://nextjs.org/) project bootstrapped using [`create-next-app`](https://github.com/vercel/next.js/tree/HEAD/packages/create-next-app) with Zero Runtime installed.
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved

## How to use

Download the example [or clone the repo](https://github.com/mui/material-ui):

<!-- #default-branch-switch -->

```bash
curl https://codeload.github.com/mui/material-ui/tar.gz/master | tar -xz --strip=2 material-ui-master/examples/zero-nextjs
cd zero-nextjs
```

Install it and run:

```bash
npm install
npm run dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

or:

<!-- #default-branch-switch -->

[![Edit on StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/mui/material-ui/tree/master/examples/zero-nextjs)

[![Edit on CodeSandbox](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/p/sandbox/github/mui/material-ui/tree/master/examples/zero-nextjs)

## Learn more

To learn more about this example:

- [Zero Runtime documentation](https://github.com/mui/material-ui/blob/master/packages/zero-runtime/README.md) - learn how to use Zero Runtime.
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
- [Next.js documentation](https://nextjs.org/docs) - learn about Next.js features and API.
5 changes: 5 additions & 0 deletions examples/zero-nextjs/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
25 changes: 25 additions & 0 deletions examples/zero-nextjs/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { withZeroPlugin } = require('@mui/zero-next-plugin');
const { extendTheme } = require('@mui/zero-runtime');
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved

const theme = extendTheme({
colorSchemes: {
light: {
palette: {
background: '0 0% 100%',
foreground: '240 10% 3.9%',
primary: '240 5.9% 10%',
border: '240 5.9% 90%',
},
},
dark: {
palette: {
background: '240 10% 3.9%',
foreground: '0 0% 80%',
primary: '0 0% 98%',
border: '240 3.7% 15.9%',
},
},
},
});

module.exports = withZeroPlugin({}, { theme });
27 changes: 27 additions & 0 deletions examples/zero-nextjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "zero-nextjs",
"version": "5.0.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"post-update": "echo \"codesandbox preview only, need an update\" && pnpm update --latest"
},
"dependencies": {
"@mui/zero-runtime": "workspace:^",
"react": "latest",
"react-dom": "latest",
"next": "latest"
},
"devDependencies": {
"@mui/zero-next-plugin": "workspace:^",
"@types/node": "latest",
"@types/react": "latest",
"@types/react-dom": "latest",
"eslint": "latest",
"eslint-config-next": "latest",
"typescript": "latest"
}
}
Empty file.
Binary file added examples/zero-nextjs/src/app/favicon.ico
Binary file not shown.
16 changes: 16 additions & 0 deletions examples/zero-nextjs/src/app/globals.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}

a {
color: inherit;
text-decoration: none;
}

@media (prefers-color-scheme: dark) {
html {
color-scheme: dark;
}
}
28 changes: 28 additions & 0 deletions examples/zero-nextjs/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as React from 'react';
import type { Metadata } from 'next';
import '@mui/zero-runtime/styles.css';
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
import { css } from '@mui/zero-runtime';

import './globals.css';

export const metadata: Metadata = {
title: 'Create Next App',
description: 'Generated by create next app',
};

export default function RootLayout(props: { children: React.ReactNode }) {
return (
<html lang="en">
<body
className={css`
color: hsl(var(--palette-foreground));
background-color: hsl(var(--palette-background));
font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif,
'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol';
`}
>
{props.children}
</body>
</html>
);
}
213 changes: 213 additions & 0 deletions examples/zero-nextjs/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
import * as React from 'react';
import { Kalnia, Josefin_Sans } from 'next/font/google';
import { styled, css, keyframes } from '@mui/zero-runtime';

const kalnia = Kalnia({ subsets: ['latin'] });
const josefin = Josefin_Sans({ subsets: ['latin'] });

const scale = keyframes`
to {
scale: var(--s2);
}
`;

const Link = styled('a', { shouldForwardProp: (prop) => prop !== 'outlined' })<{
outlined?: boolean;
}>(({ theme }) => ({
fontSize: '1rem',
background: 'rgba(0 0 0 / 0.04)',
padding: '0.5rem 1rem',
letterSpacing: '1px',
borderRadius: '4px',
textAlign: 'center',
...theme.applyStyles('dark', {
background: 'rgba(255 255 255 / 0.1)',
}),
variants: [
{
props: { outlined: true },
style: {
background: 'transparent',
color: `hsl(${theme.vars.palette.primary})`,
border: `1px solid hsl(${theme.vars.palette.border})`,
},
},
],
}));

const Bubble = styled('span')({
height: 'var(--size, 100%)',
aspectRatio: '1',
background: 'radial-gradient(hsl(var(--h) 100% 70%) 25%, transparent 50%)',
position: 'absolute',
display: 'inline-block',
left: 'var(--x, 0)',
top: 'var(--y, 0)',
scale: '0',
translate: '-50% -50%',
mixBlendMode: 'multiply',
filter: 'blur(2px)',
animation: `${scale} var(--s, 2s) var(--d, 0s) infinite alternate`,
});

function randomBetween(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function generateBubbleVars() {
return `
--x: ${randomBetween(10, 90)}%;
--y: ${randomBetween(15, 85)}%;
--h: ${randomBetween(0, 360)};
--s2: ${randomBetween(2, 6)};
--d: -${randomBetween(250, 400) / 1000}s;
--s: ${randomBetween(3, 6)}s;
`;
}

export default function Home() {
return (
<main
className={css({
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
height: '100lvh',
padding: '20px',
})}
>
<h1
className={`
${kalnia.className}
${css`
font-size: clamp(3rem, 1.385rem + 6.8906vw, 8rem);
font-weight: 500;
line-height: 1;
text-align: center;
position: relative;
display: flex;
align-items: center;
color: #888;
margin-bottom: 1rem;

@media (prefers-color-scheme: dark) {
color: #fff;
}
`}`}
>
Zero Runtime
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
<span
className={css`
position: absolute;
inset: 0;
background: white;
mix-blend-mode: color-burn;
overflow: hidden;
pointer-events: none;

@media (prefers-color-scheme: dark) {
mix-blend-mode: darken;
filter: brightness(2);
}
`}
>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
Copy link
Contributor

@brijeshb42 brijeshb42 Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an FYI, These variables will be random during the build time. In the browser, it'll still be static.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's the intention.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's worth mentioning this behavior between emotion/zero-runtime:

{[…Array(10)].map((_, index) => (
    <Bubble
      key={index}
      className={css`
        ${generateBubbleVars()}
      `
    />
)}

Zero-runtime: generate just one set of variables (all instances will have the same styles).
Emotion: generate 10 random set of styles.

Copy link
Contributor

@brijeshb42 brijeshb42 Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zero-runtime: generate just one set of variables that'll only change when you rebuild the app. And for each build, it'll not change with page refresh.

Emotion: generate 10 random set of styles that'll also change with each page refresh.

`}
/>
</span>
</h1>
<div
className={`
${josefin.className}
${css`
font-size: clamp(0.75rem, 0.5885rem + 0.6891vw, 1.25rem);
letter-spacing: 2px;
text-transform: uppercase;
font-weight: 500;
opacity: 0.5;
line-height: 2;
text-align: center;
text-wrap: balance;
`}
`}
>
CSS-JS library with static extraction
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
</div>

<div
className={css`
display: flex;
flex-wrap: wrap;
gap: 1rem;
margin-top: 3rem;
& > * {
flex: auto;
}
`}
>
<Link
href="https://github.com/mui/material-ui/blob/master/packages/zero-runtime/README.md"
target="_blank"
rel="noopener noreferrer"
>
Documentation
</Link>
<Link
outlined
href="https://github.com/orgs/mui/projects/27/views/1"
target="_blank"
rel="noopener noreferrer"
>
Roadmap
</Link>
</div>
</main>
);
}
Loading
Loading