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

feat(styles): add antd-style example #553

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
84 changes: 84 additions & 0 deletions antd-style/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* This is intended to be a basic starting point for linting in your app.
* It relies on recommended configs out of the box for simplicity, but you can
* and should modify this configuration to best suit your team's needs.
*/

/** @type {import('eslint').Linter.Config} */
module.exports = {
root: true,
parserOptions: {
ecmaVersion: "latest",
sourceType: "module",
ecmaFeatures: {
jsx: true,
},
},
env: {
browser: true,
commonjs: true,
es6: true,
},
ignorePatterns: ["!**/.server", "!**/.client"],

// Base config
extends: ["eslint:recommended"],

overrides: [
// React
{
files: ["**/*.{js,jsx,ts,tsx}"],
plugins: ["react", "jsx-a11y"],
extends: [
"plugin:react/recommended",
"plugin:react/jsx-runtime",
"plugin:react-hooks/recommended",
"plugin:jsx-a11y/recommended",
],
settings: {
react: {
version: "detect",
},
formComponents: ["Form"],
linkComponents: [
{ name: "Link", linkAttribute: "to" },
{ name: "NavLink", linkAttribute: "to" },
],
"import/resolver": {
typescript: {},
},
},
},

// Typescript
{
files: ["**/*.{ts,tsx}"],
plugins: ["@typescript-eslint", "import"],
parser: "@typescript-eslint/parser",
settings: {
"import/internal-regex": "^~/",
"import/resolver": {
node: {
extensions: [".ts", ".tsx"],
},
typescript: {
alwaysTryTypes: true,
},
},
},
extends: [
"plugin:@typescript-eslint/recommended",
"plugin:import/recommended",
"plugin:import/typescript",
],
},

// Node
{
files: [".eslintrc.cjs"],
env: {
node: true,
},
},
],
};
5 changes: 5 additions & 0 deletions antd-style/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules

/.cache
/build
.env
20 changes: 20 additions & 0 deletions antd-style/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Ant Design Example (antd-style)

Using [antd-style](https://github.com/ant-design/antd-style) to extract styles and inject them into the HTML can avoid page flicker.

## Preview

Open this example on [CodeSandbox](https://codesandbox.com):

[![Open in CodeSandbox](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/github/remix-run/examples/tree/main/antd-style)

## Example

This example shows how to use Ant Design with Remix.

Check the content of [./app/routes/_index.tsx](./app/routes/_index.tsx) and use `<ConfigProvider />` to implement local theme changes.

## Related Links

[Ant Design](https://ant.design)
[antd-style](https://github.com/ant-design/antd-style)
37 changes: 37 additions & 0 deletions antd-style/app/entry.server.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { extractStaticStyle, StyleProvider } from "antd-style";
import { createCache } from '@ant-design/cssinjs';
import type { EntryContext } from "@remix-run/node";
import { RemixServer } from "@remix-run/react";
import { renderToString } from "react-dom/server";


export default function handleRequest(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
remixContext: EntryContext,
) {

const antdCache = createCache();

let html = renderToString(
<StyleProvider cache={antdCache}>
<RemixServer
context={remixContext}
url={request.url}
/>
</StyleProvider>
)

const styles = extractStaticStyle(html, { antdCache })
.map((item) => item.tag);

html = html.replace("__ANTD__STYLES__", styles.join("\n"));

responseHeaders.set("Content-Type", "text/html");

return new Response(`<!DOCTYPE html>${html}`, {
status: responseStatusCode,
headers: responseHeaders,
});
}
30 changes: 30 additions & 0 deletions antd-style/app/root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {
Links,
Meta,
Outlet,
Scripts,
ScrollRestoration,
} from "@remix-run/react";

export function Layout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
{typeof document === "undefined" ? "__ANTD__STYLES__" : null}
<Meta />
<Links />
</head>
<body>
{children}
<ScrollRestoration />
<Scripts />
</body>
</html>
);
}

export default function App() {
return <Outlet />;
}
64 changes: 64 additions & 0 deletions antd-style/app/routes/_index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Link } from '@remix-run/react';
import { Button, Flex, ConfigProvider } from 'antd';
import type { ThemeConfig } from "antd";

const links = [
{
href: "https://remix.run/docs",
text: "Remix Docs",
},
{
href: "https://github.com/ant-design/ant-design",
text: "Ant Design Repository"
},
{
href: "https://ant.design/",
text: "Ant Design Docs",
},
{
href: "https://github.com/ant-design/antd-style",
text: "antd-style Repository"
},
];

const theme: ThemeConfig = {
token: {
fontSize: 14,
colorPrimary: "#10b981",
},
components: {
Button: {
fontWeight: 400,
},
},
};

const Demo = () => (
<Flex gap="middle" justify='center'>
<Button type="primary">Primary Button</Button>
<Button color="default" variant="solid">Solid</Button>
</Flex>
)

export default function Index() {
return (
<Flex className="App" vertical gap="large">
<Demo />

<ConfigProvider theme={theme}>
<Demo />
</ConfigProvider>

<Flex gap="middle" vertical align='center'>
{
links.map(({ text, ...rest }) => (
<Button key={rest.href} {...rest} type="link">
{text}
</Button>
))
}
<Link to="/about">/About</Link>
</Flex>
</Flex>
);
}
33 changes: 33 additions & 0 deletions antd-style/app/routes/about.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react';
import { Alert } from 'antd';
import { createStyles } from 'antd-style';

const useStyle = createStyles(({ token, css }) => ({
title: css({
display: 'inline-block',
fontSize: token.fontSizeLG * 2,
background: `linear-gradient(to right, ${token.colorError}, ${token.colorWarning})`,
WebkitBackgroundClip: 'text',
WebkitTextFillColor: 'transparent',
})
}))

const Title = (props: React.HTMLAttributes<HTMLDivElement>) => (
<div {...props}>
ANTD-STYLE
</div>
)

const App: React.FC = () => {
const { styles } = useStyle();

return (
<Alert
message={<Title className={styles.title} />}
type="info"
description="css-in-js library with antd v5 token system"
/>
)
}

export default App;
42 changes: 42 additions & 0 deletions antd-style/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"private": true,
"sideEffects": false,
"type": "module",
"scripts": {
"build": "remix vite:build",
"dev": "remix vite:dev",
"lint": "eslint --ignore-path .gitignore --cache --cache-location ./node_modules/.cache/eslint .",
"start": "remix-serve ./build/server/index.js",
"typecheck": "tsc"
},
"dependencies": {
"@ant-design/cssinjs": "^1.22.0",
"@remix-run/node": "^2.9.2",
"@remix-run/react": "^2.9.2",
"@remix-run/serve": "^2.9.2",
"antd": "^5.22.1",
"antd-style": "^3.7.1",
"isbot": "^4.1.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@remix-run/dev": "^2.9.2",
"@types/react": "^18.2.20",
"@types/react-dom": "^18.2.7",
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4",
"eslint": "^8.38.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-jsx-a11y": "^6.7.1",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"typescript": "^5.1.6",
"vite": "^5.1.0",
"vite-tsconfig-paths": "^4.2.1"
},
"engines": {
"node": ">=20.0.0"
}
}
Binary file added antd-style/public/favicon.ico
Binary file not shown.
7 changes: 7 additions & 0 deletions antd-style/sandbox.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"hardReloadOnChange": true,
"template": "remix",
"container": {
"port": 3000
}
}
32 changes: 32 additions & 0 deletions antd-style/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"include": [
"**/*.ts",
"**/*.tsx",
"**/.server/**/*.ts",
"**/.server/**/*.tsx",
"**/.client/**/*.ts",
"**/.client/**/*.tsx"
],
"compilerOptions": {
"lib": ["DOM", "DOM.Iterable", "ES2022"],
"types": ["@remix-run/node", "vite/client"],
"isolatedModules": true,
"esModuleInterop": true,
"jsx": "react-jsx",
"module": "ESNext",
"moduleResolution": "Bundler",
"resolveJsonModule": true,
"target": "ES2022",
"strict": true,
"allowJs": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
"paths": {
"~/*": ["./app/*"]
},

// Vite takes care of building everything, not tsc.
"noEmit": true
}
}
16 changes: 16 additions & 0 deletions antd-style/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { vitePlugin as remix } from "@remix-run/dev";
import { defineConfig } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";

export default defineConfig({
plugins: [
remix({
future: {
v3_fetcherPersist: true,
v3_relativeSplatPath: true,
v3_throwAbortReason: true,
},
}),
tsconfigPaths(),
],
});