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 Ant Design example #552

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 ant-design/.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 ant-design/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules

/.cache
/build
.env
19 changes: 19 additions & 0 deletions ant-design/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Ant Design Example

Using [@ant-design/cssinjs](https://github.com/ant-design/cssinjs) 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/ant-design)

## Example

This example shows how to use Ant Desigin 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)
33 changes: 33 additions & 0 deletions ant-design/app/entry.server.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { extractStyle, StyleProvider, createCache } from "@ant-design/cssinjs";
import type { AppLoadContext, 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,
loadContext: AppLoadContext,
) {

const cache = createCache();
let markup = renderToString(
<StyleProvider cache={cache}>
<RemixServer
context={remixContext}
url={request.url}
/>
</StyleProvider>
)

markup = markup.replace("__ANTD__STYLES__", extractStyle(cache));

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

return new Response(`<!DOCTYPE html>${markup}`, {
status: responseStatusCode,
headers: responseHeaders,
});
}
30 changes: 30 additions & 0 deletions ant-design/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 />;
}
60 changes: 60 additions & 0 deletions ant-design/app/routes/_index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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",
},
];

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>
);
}
12 changes: 12 additions & 0 deletions ant-design/app/routes/about.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';
import { Alert } from 'antd';

const App: React.FC = () => (
<Alert
message="Ant Design 5.0"
type="info"
description="Help designers/developers building beautiful products more flexible and working with happiness"
/>
)

export default App;
42 changes: 42 additions & 0 deletions ant-design/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "template",
"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",
"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 ant-design/public/favicon.ico
Binary file not shown.
7 changes: 7 additions & 0 deletions ant-design/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 ant-design/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 ant-design/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(),
],
});