Skip to content

Docs: Add a new example using typescript for react-spectrum s2 #8012

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

Open
wants to merge 4 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
9 changes: 9 additions & 0 deletions examples/s2-webpack-5-typescript-example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
node_modules
build
.DS_Store
npm-debug.log
yarn-error.log
.yarnclean
.vscode
.idea
dist
55 changes: 55 additions & 0 deletions examples/s2-webpack-5-typescript-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Webpack 5 example

This is a [Webpack](https://webpack.js.org/) project with a minimal React configuration.

## Getting Started

First, run the development server:

```bash
yarn install
yarn dev
```

Open [http://localhost:8080](http://localhost:8080) with your browser to see the result.

style-macro and React Spectrum - Spectrum 2 have been added to `src/App.js` to show an example of a Spectrum 2 styled component. This file does client side rendering. The page auto-updates as you edit the file.

## Macros config

Edit the webpack.config.js to add an import for the plugin and add a webpack config that adds the webpack version of the macros plugin. An empty config file would be updated to look like the following.

```
const macros = require("unplugin-parcel-macros");

module.exports = {
// ...
plugins: [
// ...
macros.webpack(),
// ...
],
};
```

To use the spectrum-theme via macros, pass your styles object to the style() macro and set the result as a new function. This new function or style() should be used within a `className` prop to style your html elements. Use the `styles` prop on React Spectrum components.

```jsx
<div className={style({marginStart: 16})}>
Hello Spectrum 2!
</div>
```

```jsx
<Button styles={style({marginStart: 16})}>
Hello Spectrum 2!
</Button>
```

## Application setup

Please include the page level CSS in the root of your application to configure and support the light and dark themes.

```
import "@react-spectrum/s2/page.css";
```
34 changes: 34 additions & 0 deletions examples/s2-webpack-5-typescript-example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "webpack-5-typescript-example",
"version": "1.0.0",
"description": "",
"main": "./src/index.tsx",
"packageManager": "[email protected]",
"scripts": {
"dev": "webpack serve",
"build": "webpack --mode production"
},
"dependencies": {
"@react-spectrum/s2": "latest",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@swc/core": "^1.11.13",
"@types/react": "^19.0.12",
"@types/react-dom": "^19.0.4",
"css-loader": "^6.10.0",
"css-minimizer-webpack-plugin": "^7.0.0",
"html-webpack-plugin": "^5.6.0",
"lightningcss": "^1.27.0",
"mini-css-extract-plugin": "^2.9.1",
"style-loader": "^3.3.4",
"swc-loader": "^0.2.6",
"swc-minify-webpack-plugin": "^2.1.3",
"typescript": "^5.8.2",
"unplugin-parcel-macros": "0.0.3",
"webpack": "^5.91.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.0.4"
}
}
262 changes: 262 additions & 0 deletions examples/s2-webpack-5-typescript-example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
/*
* Copyright 2024 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

import React, { useState } from "react";
import "@react-spectrum/s2/page.css";
import {
ActionButton,
ActionButtonGroup,
ActionMenu,
Button,
ButtonGroup,
Cell,
Column,
Divider,
Heading,
LinkButton,
Menu,
MenuItem,
MenuTrigger,
Picker,
PickerItem,
Row,
SubmenuTrigger,
TableBody,
TableHeader,
TableView,
Text,
ToggleButton,
ToggleButtonGroup,
TreeView,
TreeViewItem,
TreeViewItemContent
} from "@react-spectrum/s2";
import Edit from "@react-spectrum/s2/icons/Edit";
import FileTxt from "@react-spectrum/s2/icons/FileText";
import Folder from "@react-spectrum/s2/icons/Folder";
import Section from "./components/Section";
import { style } from "@react-spectrum/s2/style" with { type: "macro" };
import { CardViewExample } from "./components/CardViewExample";
import { CollectionCardsExample } from "./components/CollectionCardsExample";

const Lazy = React.lazy(() => import('./Lazy'));

function App() {
let [isLazyLoaded, setLazyLoaded] = useState(false);
let [cardViewState, setCardViewState] = useState({
layout: 'grid',
loadingState: 'idle',
});
let cardViewLoadingOptions = [
{id: 'idle', label: 'Idle'},
{id: 'loading', label: 'Loading'},
{id: 'sorting', label: 'Sorting'},
{id: 'loadingMore', label: 'Loading More'},
{id: 'error', label: 'Error'},
];
let cardViewLayoutOptions = [
{id: 'grid', label: 'Grid'},
{id: 'waterfall', label: 'Waterfall'}
];
return (
<main>
<Heading
styles={style({ font: "heading-xl", textAlign: "center" })}
level={1}
>
Spectrum 2 + Webpack
</Heading>
<div
className={style({
maxWidth: 288,
margin: "auto",
})}
>
<Divider />
</div>
<div
className={style({
display: "flex",
flexDirection: "column",
gap: 16,
alignItems: "center"
})}
>
<Section title="Buttons">
<ButtonGroup align="center" styles={style({maxWidth: '[100vw]'})}>
<Button variant="primary">Primary</Button>
<Button variant="secondary">Secondary</Button>
<ActionButton>
<Edit />
<Text>Action Button</Text>
</ActionButton>
<ToggleButton>Toggle Button</ToggleButton>
<LinkButton
variant="primary"
href="https://adobe.com"
target="_blank"
>
Link Button
</LinkButton>
<ActionButtonGroup density="compact">
<ActionButton>Cut</ActionButton>
<ActionButton>Copy</ActionButton>
<ActionButton>Paste</ActionButton>
</ActionButtonGroup>
<ToggleButtonGroup density="compact" selectionMode="multiple">
<ToggleButton id="bold">Bold</ToggleButton>
<ToggleButton id="italic">Italic</ToggleButton>
<ToggleButton id="underline">Underline</ToggleButton>
</ToggleButtonGroup>
</ButtonGroup>
</Section>

<Section title="Collections">
<ActionMenu>
<MenuItem>Action Menu Item 1</MenuItem>
<MenuItem>Action Menu Item 2</MenuItem>
<MenuItem>Action Menu Item 3</MenuItem>
</ActionMenu>
<Picker
label="CardView Loading State"
items={cardViewLoadingOptions}
selectedKey={cardViewState.loadingState}
onSelectionChange={loadingState => setCardViewState({...cardViewState, loadingState} as any)}>
{item => <PickerItem id={item.id}>{item.label}</PickerItem>}
</Picker>
<Picker
label="CardView Layout"
items={cardViewLayoutOptions}
selectedKey={cardViewState.layout}
onSelectionChange={layout => setCardViewState({...cardViewState, layout} as any)}>
{item => <PickerItem id={item.id}>{item.label}</PickerItem>}
</Picker>
<CardViewExample {...cardViewState} />
<Divider styles={style({maxWidth: 320, width: 'full', marginX: 'auto'})} />
<CollectionCardsExample loadingState={cardViewState.loadingState} />
<MenuTrigger>
<ActionButton>Menu</ActionButton>
<Menu onAction={(key) => alert(key.toString())}>
<MenuItem id="cut">Cut</MenuItem>
<MenuItem id="copy">Copy</MenuItem>
<MenuItem id="paste">Paste</MenuItem>
<MenuItem id="replace">Replace</MenuItem>
<SubmenuTrigger>
<MenuItem id="share">Share</MenuItem>
<Menu onAction={(key) => alert(key.toString())}>
<MenuItem id="copy-ink">Copy Link</MenuItem>
<SubmenuTrigger>
<MenuItem id="email">Email</MenuItem>
<Menu onAction={(key) => alert(key.toString())}>
<MenuItem id="attachment">Email as Attachment</MenuItem>
<MenuItem id="link">Email as Link</MenuItem>
</Menu>
</SubmenuTrigger>
<MenuItem id="sms">SMS</MenuItem>
</Menu>
</SubmenuTrigger>
<MenuItem id="delete">Delete</MenuItem>
</Menu>
</MenuTrigger>
<MenuTrigger>
<ActionButton>Menu Trigger</ActionButton>
<Menu>
<MenuItem href="/foo" routerOptions={{ scroll: false } as any}>
Link to /foo
</MenuItem>
<MenuItem>Cut</MenuItem>
<MenuItem>Copy</MenuItem>
<MenuItem>Paste</MenuItem>
</Menu>
</MenuTrigger>
<TableView aria-label="Files" styles={style({width: 320, height: 320})}>
<TableHeader>
<Column isRowHeader>Name</Column>
<Column>Type</Column>
<Column>Date Modified</Column>
<Column>A</Column>
<Column>B</Column>
</TableHeader>
<TableBody>
<Row id="1">
<Cell>Games</Cell>
<Cell>File folder</Cell>
<Cell>6/7/2020</Cell>
<Cell>Dummy content</Cell>
<Cell>Long long long long long long long cell</Cell>
</Row>
<Row id="2">
<Cell>Program Files</Cell>
<Cell>File folder</Cell>
<Cell>4/7/2021</Cell>
<Cell>Dummy content</Cell>
<Cell>Long long long long long long long cell</Cell>
</Row>
<Row id="3">
<Cell>bootmgr</Cell>
<Cell>System file</Cell>
<Cell>11/20/2010</Cell>
<Cell>Dummy content</Cell>
<Cell>Long long long long long long long cell</Cell>
</Row>
</TableBody>
</TableView>
<TreeView disabledKeys={['projects-1']} aria-label="test static tree">
<TreeViewItem id="Photos" textValue="Photos">
<TreeViewItemContent>
<Text>Photos</Text>
<Folder />
</TreeViewItemContent>
</TreeViewItem>
<TreeViewItem id="projects" textValue="Projects">
<TreeViewItemContent>
<Text>Projects</Text>
<Folder />
</TreeViewItemContent>
<TreeViewItem id="projects-1" textValue="Projects-1">
<TreeViewItemContent>
<Text>Projects-1</Text>
<Folder />
</TreeViewItemContent>
<TreeViewItem id="projects-1A" textValue="Projects-1A">
<TreeViewItemContent>
<Text>Projects-1A</Text>
<FileTxt />
</TreeViewItemContent>
</TreeViewItem>
</TreeViewItem>
<TreeViewItem id="projects-2" textValue="Projects-2">
<TreeViewItemContent>
<Text>Projects-2</Text>
<FileTxt />
</TreeViewItemContent>
</TreeViewItem>
<TreeViewItem id="projects-3" textValue="Projects-3">
<TreeViewItemContent>
<Text>Projects-3</Text>
<FileTxt />
</TreeViewItemContent>
</TreeViewItem>
</TreeViewItem>
</TreeView>
</Section>

{!isLazyLoaded && <ActionButton onPress={() => setLazyLoaded(true)}>Load more</ActionButton>}
{isLazyLoaded && <React.Suspense fallback={<>Loading</>}>
<Lazy />
</React.Suspense>}
</div>
</main>
);
}

export default App;
Loading