Skip to content

Commit 8d13969

Browse files
committed
chore: add lib-react example
1 parent 8dec625 commit 8d13969

10 files changed

Lines changed: 228 additions & 0 deletions

File tree

examples/lib-react/package.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "@examples/lib-react",
3+
"private": true,
4+
"files": [
5+
"dist"
6+
],
7+
"type": "module",
8+
"types": "./dist/index.d.ts",
9+
"exports": {
10+
".": {
11+
"types": "./dist/index.d.ts",
12+
"import": "./dist/index.js"
13+
}
14+
},
15+
"scripts": {
16+
"build": "rs lib",
17+
"dev": "rs lib -w",
18+
"lint": "rs lint",
19+
"test": "rs test",
20+
"test:watch": "rs test -w"
21+
},
22+
"devDependencies": {
23+
"@rsbuild/plugin-react": "catalog:",
24+
"@rstest/adapter-rslib": "catalog:",
25+
"@testing-library/dom": "catalog:",
26+
"@testing-library/jest-dom": "catalog:",
27+
"@testing-library/react": "catalog:",
28+
"@types/react": "catalog:",
29+
"@types/react-dom": "catalog:",
30+
"happy-dom": "catalog:",
31+
"react": "catalog:",
32+
"react-dom": "catalog:",
33+
"rstack": "workspace:*",
34+
"typescript": "catalog:"
35+
},
36+
"peerDependencies": {
37+
"react": ">=18.0.0",
38+
"react-dom": ">=18.0.0"
39+
}
40+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { define } from 'rstack';
2+
3+
define.lib(async () => {
4+
const { pluginReact } = await import('@rsbuild/plugin-react');
5+
return {
6+
source: {
7+
entry: {
8+
index: ['./src/**'],
9+
},
10+
},
11+
lib: [{ bundle: false, dts: true }],
12+
output: {
13+
target: 'web',
14+
},
15+
plugins: [pluginReact()],
16+
};
17+
});
18+
19+
define.test({
20+
setupFiles: ['./tests/rstest.setup.ts'],
21+
});
22+
23+
define.lint(async () => {
24+
const { js, ts, reactPlugin, reactHooksPlugin } = await import('rstack/lint');
25+
return [
26+
js.configs.recommended,
27+
ts.configs.recommended,
28+
reactPlugin.configs.recommended,
29+
reactHooksPlugin.configs.recommended,
30+
];
31+
});

examples/lib-react/src/Button.tsx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import './button.css';
2+
3+
export interface ButtonProps {
4+
/**
5+
* Whether the button is primary
6+
* @default false
7+
*/
8+
primary?: boolean;
9+
/**
10+
* Background color of the button
11+
*/
12+
backgroundColor?: string;
13+
/**
14+
* Size of Button
15+
* @default 'medium'
16+
*/
17+
size?: 'small' | 'medium' | 'large';
18+
/**
19+
* Label of the button
20+
*/
21+
label: string;
22+
/**
23+
* Optional click handler
24+
*/
25+
onClick?: () => void;
26+
}
27+
28+
export const Button = ({
29+
primary = false,
30+
size = 'medium',
31+
backgroundColor,
32+
label,
33+
...props
34+
}: ButtonProps) => {
35+
const mode = primary ? 'demo-button--primary' : 'demo-button--secondary';
36+
return (
37+
<button
38+
type="button"
39+
className={['demo-button', `demo-button--${size}`, mode].join(' ')}
40+
style={{ backgroundColor }}
41+
{...props}
42+
>
43+
{label}
44+
</button>
45+
);
46+
};

examples/lib-react/src/button.css

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
.demo-button {
2+
font-weight: 700;
3+
border: 0;
4+
border-radius: 3em;
5+
cursor: pointer;
6+
display: inline-block;
7+
line-height: 1;
8+
}
9+
10+
.demo-button--primary {
11+
color: white;
12+
background-color: #1ea7fd;
13+
}
14+
15+
.demo-button--secondary {
16+
color: #333;
17+
background-color: transparent;
18+
box-shadow: rgba(0, 0, 0, 0.15) 0px 0px 0px 1px inset;
19+
}
20+
21+
.demo-button--small {
22+
font-size: 12px;
23+
padding: 10px 16px;
24+
}
25+
26+
.demo-button--medium {
27+
font-size: 14px;
28+
padding: 11px 20px;
29+
}
30+
31+
.demo-button--large {
32+
font-size: 16px;
33+
padding: 12px 24px;
34+
}

examples/lib-react/src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { Button } from './Button';
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { expect, test } from 'rstack/test';
2+
import { render, screen } from '@testing-library/react';
3+
import { Button } from '../src/Button';
4+
5+
test('The button should have correct background color', async () => {
6+
render(<Button backgroundColor="#ccc" label="Demo Button" />);
7+
const button = screen.getByText('Demo Button');
8+
expect(button).toHaveStyle({
9+
backgroundColor: '#ccc',
10+
});
11+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { expect } from 'rstack/test';
2+
import * as jestDomMatchers from '@testing-library/jest-dom/matchers';
3+
4+
expect.extend(jestDomMatchers);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"rootDir": "..",
5+
"types": ["@testing-library/jest-dom"]
6+
},
7+
"include": ["./"]
8+
}

examples/lib-react/tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["DOM", "ES2022"],
4+
"jsx": "react-jsx",
5+
"skipLibCheck": true,
6+
"types": ["rstack/types", "node"],
7+
"isolatedModules": true,
8+
"resolveJsonModule": true,
9+
"moduleResolution": "bundler",
10+
"useDefineForClassFields": true,
11+
"rootDir": "src"
12+
},
13+
"include": ["src"]
14+
}

pnpm-lock.yaml

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)