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-vite-ts starter example #41196

Merged
merged 10 commits into from Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions examples/pigmentcss-vite-ts/.gitignore
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
34 changes: 34 additions & 0 deletions examples/pigmentcss-vite-ts/README.md
@@ -0,0 +1,34 @@
# Zero-runtime CSS-in-JS - Vite with TypeScript example project

## 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/pigmentcss-vite-ts
cd pigmentcss-vite-ts
```

Install it and run:

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

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/pigmentcss-vite-ts)

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

## Learn more

To learn more about this example:

- [`@pigmentcss/react` documentation](https://github.com/mui/material-ui/blob/master/packages/pigment-react/README.md) - learn more about MUI's Pigment CSS features.
- [Vite documentation](https://vitejs.dev/guide/) - learn about Vite features and API.
20 changes: 20 additions & 0 deletions examples/pigmentcss-vite-ts/index.html
@@ -0,0 +1,20 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="initial-scale=1, width=device-width" />
<!-- Fonts to support Material Design -->
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Josefin+Sans:ital,wght@0,100..700;1,100..700&family=Kalnia:wght@100..700&display=swap"
rel="stylesheet"
/>
<title>Vite + Zero + TS</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
24 changes: 24 additions & 0 deletions examples/pigmentcss-vite-ts/package.json
@@ -0,0 +1,24 @@
{
"name": "zero-vite",
"private": true,
"version": "5.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"@pigmentcss/react": "latest",
Copy link
Contributor

Choose a reason for hiding this comment

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

We have renamed the package, again :)

"react": "latest",
"react-dom": "latest"
},
"devDependencies": {
"@pigmentcss/vite-plugin": "latest",
Copy link
Contributor

@brijeshb42 brijeshb42 Mar 4, 2024

Choose a reason for hiding this comment

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

Same

"@types/react": "latest",
"@types/react-dom": "latest",
"@vitejs/plugin-react": "latest",
"typescript": "latest",
"vite": "latest"
}
}
1 change: 1 addition & 0 deletions examples/pigmentcss-vite-ts/public/vite.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
202 changes: 202 additions & 0 deletions examples/pigmentcss-vite-ts/src/App.tsx
@@ -0,0 +1,202 @@
import * as React from 'react';
import { styled, css, keyframes } from '@pigmentcss/react';

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',
color: 'hsl(var(--palette-foreground))',
backgroundColor: 'hsl(var(--palette-background))',
fontFamily:
"system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'",
})}
>
<h1
className={`
${css(({ theme }) => ({
fontFamily: 'Kalnia, sans-serif',
fontSize: 'clamp(3rem, 1.9503rem + 4.4789vw, 6.25rem)',
fontWeight: 500,
textAlign: 'center',
position: 'relative',
display: 'flex',
alignItems: 'center',
color: '#888',
marginBottom: '1rem',
...theme.applyStyles('dark', { color: '#fff' }),
}))}`}
>
Pigment CSS
<span
className={css(({ theme }) => ({
position: 'absolute',
inset: '0',
background: 'white',
mixBlendMode: 'color-burn',
overflow: 'hidden',
pointerEvents: 'none',
...theme.applyStyles('dark', {
mixBlendMode: '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()}
`}
/>
</span>
</h1>
<div
className={css({
fontFamily: 'Josefin Sans, sans-serif',
letterSpacing: '2px',
fontSize: 'clamp(0.75rem, 0.5885rem + 0.6891vw, 1.25rem)',
textTransform: 'uppercase',
fontWeight: 500,
opacity: 0.5,
lineHeight: 2,
textAlign: 'center',
textWrap: 'balance',
})}
>
CSS-JS library with static extraction
</div>

<div
className={css({
display: 'flex',
flexWrap: 'wrap',
gap: '1rem',
marginTop: '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>
);
}
19 changes: 19 additions & 0 deletions examples/pigmentcss-vite-ts/src/augment.d.ts
@@ -0,0 +1,19 @@
import type {} from '@pigmentcss/react/theme';
import type { ExtendTheme } from '@pigmentcss/react';

declare module '@pigmentcss/react/theme' {
export interface ThemeArgs {
theme: ExtendTheme<{
colorScheme: 'light' | 'dark';
tokens: {
palette: {
background: string;
foreground: string;
primary: string;
primaryForeground: string;
border: string;
};
};
}>;
}
}
16 changes: 16 additions & 0 deletions examples/pigmentcss-vite-ts/src/globals.css
@@ -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;
}
}
11 changes: 11 additions & 0 deletions examples/pigmentcss-vite-ts/src/main.tsx
@@ -0,0 +1,11 @@
import * as React from 'react';
import * as ReactDOM from 'react-dom/client';
import '@pigmentcss/react/styles.css';
import './globals.css';
import App from './App';

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<App />
</React.StrictMode>,
);
1 change: 1 addition & 0 deletions examples/pigmentcss-vite-ts/src/vite-env.d.ts
@@ -0,0 +1 @@
/// <reference types="vite/client" />
21 changes: 21 additions & 0 deletions examples/pigmentcss-vite-ts/tsconfig.json
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"allowJs": false,
"skipLibCheck": true,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}
9 changes: 9 additions & 0 deletions examples/pigmentcss-vite-ts/tsconfig.node.json
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"composite": true,
"module": "ESNext",
"moduleResolution": "Node",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts"]
}