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

[webpack] Fix support for webpack and add example app #174

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 24 additions & 0 deletions examples/pigment-css-webpack-ts/.gitignore
Original file line number Diff line number Diff line change
@@ -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/pigment-css-webpack-ts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Pigment CSS - Vite with TypeScript example project

## How to use

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

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

```bash
curl https://codeload.github.com/mui/pigment-css/tar.gz/master | tar -xz --strip=2 pigment-css-master/examples/pigment-css-vite-ts
cd pigment-css-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/pigment-css/tree/master/examples/pigment-css-vite-ts)

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

## Learn more

To learn more about this example:

- [Pigment CSS documentation](https://github.com/mui/pigment-css/blob/master/README.md) - learn more about Pigment CSS features and APIs.
- [Vite documentation](https://vitejs.dev/guide/) - learn about Vite features and APIs.
Copy link
Member

Choose a reason for hiding this comment

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

It should reference webpack on this demo. #277

11 changes: 11 additions & 0 deletions examples/pigment-css-webpack-ts/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1, width=device-width" />
<title>Pigment CSS + Webpack + TypeScript</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
28 changes: 28 additions & 0 deletions examples/pigment-css-webpack-ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@app/wp-app",
"private": true,
"type": "module",
"scripts": {
"dev": "webpack serve --mode development",
"build": "webpack build",
"preview": "vite preview"
},
"dependencies": {
"@pigment-css/react": "latest",
"react": "latest",
"react-dom": "latest"
},
"devDependencies": {
"@pigment-css/unplugin": "latest",
"@types/react": "latest",
"@types/react-dom": "latest",
"css-loader": "^7.1.2",
"html-webpack-plugin": "^5.6.0",
"style-loader": "^4.0.0",
"ts-loader": "^9.5.1",
"typescript": "latest",
"webpack": "^5.91.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.0.4"
}
}
197 changes: 197 additions & 0 deletions examples/pigment-css-webpack-ts/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
import * as React from 'react';
import { styled, css, keyframes } from '@pigment-css/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.8rem 1rem',
letterSpacing: '1px',
borderRadius: '8px',
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={`my-custom-class ${css(({ theme }) => ({
fontFamily: 'system-ui, sans-serif',
fontSize: '4rem',
fontWeight: 500,
textAlign: 'center',
position: 'relative',
display: 'flex',
alignItems: 'center',
color: '#888',
marginBottom: '1rem',
...theme.applyStyles('dark', { color: '#fff' }),
}))}`}
>
Pigment&nbsp;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: 'system-ui, sans-serif',
letterSpacing: '2px',
opacity: 0.6,
lineHeight: 2,
textAlign: 'center',
textWrap: 'balance',
})}
>
CSS-in-JS library with static extraction
</div>
<div
className={css({
display: 'flex',
flexWrap: 'wrap',
gap: '1rem',
marginTop: '2rem',
})}
>
<Link
href="https://github.com/mui/pigment-css/blob/master/README.md"
target="_blank"
rel="noopener noreferrer"
>
Documentation
</Link>
<Link
outlined
href="https://github.com/orgs/mui/projects/27/views/3"
target="_blank"
rel="noopener noreferrer"
>
Roadmap
</Link>
</div>
</main>
);
}
19 changes: 19 additions & 0 deletions examples/pigment-css-webpack-ts/src/augment.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type {} from '@pigment-css/react/theme';
import type { ExtendTheme } from '@pigment-css/react';

declare module '@pigment-css/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/pigment-css-webpack-ts/src/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;
}
}
11 changes: 11 additions & 0 deletions examples/pigment-css-webpack-ts/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as React from 'react';
import * as ReactDOM from 'react-dom/client';
import '@pigment-css/react/styles.css';
import './globals.css';
import App from './App';

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<App />
</React.StrictMode>,
);
21 changes: 21 additions & 0 deletions examples/pigment-css-webpack-ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"outDir": "./dist/",
"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,
"jsx": "react-jsx"
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}
8 changes: 8 additions & 0 deletions examples/pigment-css-webpack-ts/tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"composite": true,
"module": "ESNext",
"moduleResolution": "Node",
"allowSyntheticDefaultImports": true
}
}
Loading
Loading