### Gallery Development Commands
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/CLAUDE.md
Commands for developing the gallery application, which showcases component usage. Includes installation, starting the dev server, building, testing, and linting.
```bash
yarn install
yarn start
yarn build
yarn test
yarn lint
```
--------------------------------
### Grid Layout Examples
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/gallery/src/styles/NxGrid/NxGridExample.html
Demonstrates various grid layouts using percentage-based column widths. These examples showcase different combinations of column sizes to create responsive layouts.
```html
...
...
...
...
...
...
...
...
...
...
```
--------------------------------
### React 19 with TypeScript Setup
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/CLAUDE.md
Demonstrates the basic setup for a React 19 project using TypeScript, including necessary dependencies and configuration hints.
```typescript
import React from 'react';
import ReactDOM from 'react-dom/client';
const App: React.FC = () => {
return (
Welcome to React 19!
);
};
const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement
);
root.render();
```
```json
{
"compilerOptions": {
"target": "es2020",
"lib": ["dom", "dom.iterable", "esnext"],
"jsx": "react-jsx",
"module": "esnext",
"moduleResolution": "node",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src"]
}
```
--------------------------------
### Install React Shared Components
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/README.md
Demonstrates how to install the `@sonatype/react-shared-components` npm package using Yarn.
```bash
yarn add @sonatype/react-shared-components
```
--------------------------------
### Run Next.js Development Server
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/ssr-tests/README.md
Starts the Next.js development server for manual testing of the application. Assumes dependencies have been installed.
```bash
yarn dev
```
--------------------------------
### Typical Development Setup
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/CLAUDE.md
Instructions for setting up the development environment by running the library's watch mode and the gallery's development server simultaneously.
```bash
# In lib/
yarn clean && rm -rf node_modules && yarn install && yarn watch
# In gallery/
yarn clean && rm -rf node_modules && yarn install && yarn start
```
--------------------------------
### SCSS Styling Example
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/CLAUDE.md
An example of how SCSS can be used for styling React components, demonstrating basic nesting and variables.
```scss
$primary-color: #007bff;
.button {
background-color: $primary-color;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
&:hover {
background-color: darken($primary-color, 10%);
}
}
```
```typescript
import React from 'react';
import './Button.scss';
interface ButtonProps {
children: React.ReactNode;
}
const Button: React.FC = ({ children }) => {
return ;
};
export default Button;
```
--------------------------------
### Component Gallery Setup and Running
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/README.md
Instructions for building the component library and running the component gallery locally. This allows developers to view and interact with components in a browser.
```bash
yarn run build
# or
yarn run watch
```
```bash
cd gallery/
yarn install
```
```bash
cd gallery/
yarn start
```
--------------------------------
### Build Process and Dependencies
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/README.md
Details the software requirements and installation steps for building the Sonatype React Shared Components library.
```markdown
### Required Software
* Node 20.x
* yarn 1.22.x
* git-lfs for visual test snapshots. For the command line git client, git-lfs is a separate program which functions as a
plugin; for graphical git clients, git-lfs support is often built-in. To check whether your checkout used git-lfs
successfully, check whether the files within `gallery/visualtests/__image_snapshots__` are actual pngs as opposed to
stub text files.
### Installation of Dependencies
In the lib/ directory, run `yarn install`
### Build
For a one-time build, `yarn run build` in the lib/ directory.
When developing, you probably want to use `yarn run watch` instead.
```
--------------------------------
### Library Development Commands
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/CLAUDE.md
Commands for developing the React Shared Components library, including installation, building, testing, and linting.
```bash
yarn install
yarn build
yarn watch
yarn test
yarn test-watch
yarn jest
yarn lint
yarn ci-lint
yarn clean
```
--------------------------------
### Docker Selenium Chrome Setup
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/ssr-tests/README.md
Starts a Docker container for Selenium Standalone Chrome, exposing port 4444 and mounting /dev/shm for shared memory, which is often required by Chrome in containerized environments.
```bash
docker run --name selenium-chrome -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome:127-20250202
```
--------------------------------
### Run Gallery and Build Shared Components
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/gallery/README.md
Instructions for running the component gallery and building the shared component library. Running `yarn start` launches the gallery with webpack dev server. To see changes in shared components, run `npm run build` or `npm run watch` from the ../lib/ directory.
```bash
yarn start
npm run build
# or
npm run watch
```
--------------------------------
### Dark Mode Toggle and Force Light Mode
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/gallery/src/styles/DarkMode/DarkModeLightOverrideExample.html
This example shows how to create a toggle for dark mode and a button to force the application into light mode using React hooks and context. It assumes a context provider is set up to manage the theme state.
```javascript
import React, { useContext } from 'react';
import { ThemeContext } from './ThemeContext'; // Assuming ThemeContext is defined elsewhere
function ThemeToggle() {
const { theme, toggleTheme, forceLightMode } = useContext(ThemeContext);
return (
Current theme: {theme}
);
}
export default ThemeToggle;
// ThemeContext.js (Example implementation)
/*
import React, { createContext, useState, useEffect } from 'react';
export const ThemeContext = createContext();
export const ThemeProvider = ({ children }) => {
const [theme, setTheme] = useState('light'); // Default theme
useEffect(() => {
const storedTheme = localStorage.getItem('theme');
if (storedTheme) {
setTheme(storedTheme);
}
}, []);
const toggleTheme = () => {
const newTheme = theme === 'light' ? 'dark' : 'light';
setTheme(newTheme);
localStorage.setItem('theme', newTheme);
};
const forceLightMode = () => {
setTheme('light');
localStorage.setItem('theme', 'light');
};
return (
{children}
);
};
*/
```
--------------------------------
### Emotion Styling Example
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/CLAUDE.md
Shows how to use Emotion, a CSS-in-JS library, for dynamic and component-level styling in React.
```javascript
/** @jsxImportSource @emotion/react */
import React from 'react';
import { css } from '@emotion/react';
const buttonStyle = css`
background-color: hotpink;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
&:hover {
background-color: deeppink;
}
`;
interface EmotionButtonProps {
children: React.ReactNode;
}
const EmotionButton: React.FC = ({ children }) => {
return ;
};
export default EmotionButton;
```
--------------------------------
### Primary Button Example
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/gallery/src/styles/NxBtn/NxBtnPrimaryExample.html
Demonstrates the usage of the primary button component. This is the default state for a clickable primary action.
```javascript
import React from 'react';
import { Button } from '@sonatype/react-shared-components';
function App() {
return (
);
}
```
```html
```
--------------------------------
### Run Tests and Update Screenshots on Windows
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/README.md
Provides commands for Windows users to run tests and update screenshots (light and dark modes) using Docker. These commands ensure the execution environment matches the CI setup.
```bash
docker run --rm -it -w /home/jenkins/gallery -v %CD%:/home/jenkins docker-all.repo.sonatype.com/sonatype/react-shared-components-ci:latest yarn test
```
```bash
docker run --rm -it -w /home/jenkins/gallery -v %CD%:/home/jenkins docker-all.repo.sonatype.com/sonatype/react-shared-components-ci:latest yarn jest -u
```
```bash
docker run --rm -it -w /home/jenkins/gallery -v %CD%:/home/jenkins docker-all.repo.sonatype.com/sonatype/react-shared-components-ci:latest yarn jest-dark -u
```
```bash
docker run --rm -it -w /home/jenkins/gallery -v $pwd\:/home/jenkins docker-all.repo.sonatype.com/sonatype/react-shared-components-ci:latest yarn test
```
```bash
docker run --rm -it -w /home/jenkins/gallery -v $pwd\:/home/jenkins docker-all.repo.sonatype.com/sonatype/react-shared-components-ci:latest yarn jest -u
```
```bash
docker run --rm -it -w /home/jenkins/gallery -v $pwd\:/home/jenkins docker-all.repo.sonatype.com/sonatype/react-shared-components-ci:latest yarn jest-dark -u
```
--------------------------------
### Button with Icon Example
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/gallery/src/styles/NxBtn/NxBtnIconExample.html
Demonstrates how to use the Button component to display an icon alongside text. This includes importing the necessary component and providing the icon prop.
```javascript
import React from 'react';
import Button from '@sonatype/react-shared-components/Button';
import Icon from '@sonatype/react-shared-components/Icon';
function MyComponent() {
return (
);
}
```
```typescript
import React from 'react';
import Button, { ButtonProps } from '@sonatype/react-shared-components/Button';
import Icon from '@sonatype/react-shared-components/Icon';
const MyComponent: React.FC = () => {
return (
);
};
```
--------------------------------
### Force Dark Mode without Opt-In
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/gallery/src/styles/DarkMode/DarkModeDarkOverrideNoOptInExample.html
This example shows how to apply dark mode to your React application globally, without relying on user preferences or a toggle switch. It's useful for scenarios where dark mode is the default or required theme. This implementation assumes you have a theming context or a similar mechanism in place within your Sonatype React shared components.
```javascript
import React, { useContext } from 'react';
import { ThemeContext } from './ThemeContext'; // Assuming ThemeContext is provided by Sonatype shared components
function App() {
// Force dark mode by setting the theme value directly
const { setTheme } = useContext(ThemeContext);
React.useEffect(() => {
setTheme('dark'); // Set the theme to 'dark'
}, [setTheme]);
return (
{/* Your application content here */}
Welcome to the App
This application is currently in dark mode.
);
}
export default App;
```
--------------------------------
### Puppeteer Visual Regression Test
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/CLAUDE.md
Example of a visual regression test using Puppeteer and jest-image-snapshot to capture and compare component screenshots.
```javascript
const puppeteer = require('puppeteer');
const { toMatchImageSnapshot } = require('jest-image-snapshot');
expect.extend({ toMatchImageSnapshot });
describe('Visual Regression Tests', () => {
let browser;
let page;
beforeAll(async () => {
browser = await puppeteer.launch();
page = await browser.newPage();
await page.goto('http://localhost:3000'); // Assuming your component is served here
});
afterAll(async () => {
await browser.close();
});
test('should match Button component snapshot', async () => {
const image = await page.screenshot({ clip: { x: 0, y: 0, width: 200, height: 50 } }); // Adjust clip as needed
expect(image).toMatchImageSnapshot();
});
});
```
--------------------------------
### NX Tile with Grid Header (Truncation Example)
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/gallery/src/styles/NxTile/NxTileGridExample.html
Illustrates an NX Tile component with a grid header featuring a long title, demonstrating how the grid system handles text truncation. This is useful for maintaining layout integrity with lengthy headings.
```HTML
```
```CSS
.nx-grid-header {
display: flex;
}
.nx-grid-column {
flex: 1;
padding: 10px;
box-sizing: border-box;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
```
--------------------------------
### Stateless Component Design Example
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/README.md
Components are designed to be stateless first, accepting state and callbacks as props. A stateful wrapper can be created separately for traditional architectures. This promotes reusability in environments like Redux.
```TypeScript
interface NxCheckboxProps {
isChecked: boolean;
onToggle: () => void;
}
function NxCheckbox(props: NxCheckboxProps) {
const handleClick = () => {
props.onToggle();
};
return (
{props.isChecked ? '[x]' : '[ ]'}
);
}
// Stateful wrapper example
function StatefulNxCheckbox() {
const [checked, setChecked] = React.useState(false);
const handleToggle = () => {
setChecked(!checked);
};
return ;
}
```
--------------------------------
### Development Workflow (Linux/macOS)
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/README.md
Provides commands for setting up a development environment by running the library watch process and the gallery simultaneously.
```bash
In `lib`:
```bash
yarn clean && rm -rf node_modules && yarn install && yarn watch
```
In `gallery`:
```bash
yarn clean && rm -rf node_modules && yarn install && yarn start
```
```
--------------------------------
### Visual Testing with Docker
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/CLAUDE.md
Commands to run visual regression tests using Docker for consistent environments. Includes commands for running tests and updating snapshots in both light and dark modes.
```bash
# Run visual tests
docker run --rm -it -w /home/jenkins/gallery -v $PWD:/home/jenkins docker-all.repo.sonatype.com/sonatype/react-shared-components-ci:latest yarn test
# Update snapshots (light mode)
docker run --rm -it -w /home/jenkins/gallery -v $PWD:/home/jenkins docker-all.repo.sonatype.com/sonatype/react-shared-components-ci:latest yarn jest -u
# Update snapshots (dark mode)
docker run --rm -it -w /home/jenkins/gallery -v $PWD:/home/jenkins docker-all.repo.sonatype.com/sonatype/react-shared-components-ci:latest yarn jest-dark -u
```
--------------------------------
### Development Workflow (Windows)
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/README.md
Provides PowerShell commands for setting up a development environment on Windows by running the library watch process and the gallery simultaneously.
```powershell
In `lib`:
```powershell
yarn clean; Remove-Item -Recurse -Force -Path .\node_modules; yarn install; yarn watch
```
In `gallery`:
```powershell
yarn clean; Remove-Item -Recurse -Force -Path .\node_modules; yarn install; yarn start
```
```
--------------------------------
### Running Unit Tests
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/README.md
Instructions for executing unit tests for the Sonatype React Shared Components library, including single runs and watch mode.
```bash
Run the following commands in the lib/ directory
### Single run
`yarn run test`
### Watch in console
`yarn run test-watch`
```
--------------------------------
### Component Export Pattern
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/CLAUDE.md
Illustrates the standard pattern for exporting React components and their associated props from the library's main entry point.
```typescript
export { default as ComponentName, Props as ComponentNameProps } from './components/ComponentName/ComponentName';
```
--------------------------------
### Library Structure Overview
Source: https://github.com/sonatype/sonatype-react-shared-components/blob/main/README.md
Describes the general layout of the Sonatype React Shared Components library, including the location of source code, static assets, SCSS files, and icon handling strategies.
```markdown
* Source code lives in src/. Typescript components live within src/components
* The main entrypoint for the library is src/index.ts (which compiles to index.js). All react components
are re-exported from this file. Consuming projects should import the components here, not from their individual
source files.
* Static assets live in src/assets, which is copied wholesale to dist/assets and thus included in the package
* scss files are also all copied into dist, preserving their directory structure and thus their relative paths to
components, static assets, and each other. This facilitates a consuming project using its own scss build and
provides a sane starting point for relative path references
* Icons are intended to be done using SVG, though an icon font would also work (the same way as any other static asset).
SVG icons will be encapsulated as react components that render the icon's SVG nodes inline in the document. This
provides maximum power to style the icons. The inline rendering may be mildly duplicative, compared to a separate SVG
"sprite" in conjunction with SVG `