### Setup Primer React Environment Source: https://github.com/primer/react/blob/main/contributor-docs/CONTRIBUTING.md Run this command to set up your local environment for developing Primer React components. Ensure Node.js v20 is installed. ```sh npm run setup ``` -------------------------------- ### Install Primer React Source: https://context7.com/primer/react/llms.txt Commands to install the library using npm or yarn. ```bash npm install @primer/react # or with yarn yarn add @primer/react ``` -------------------------------- ### Install postcss-preset-primer Source: https://github.com/primer/react/blob/main/packages/postcss-preset-primer/README.md Install the package as a development dependency using npm. ```bash npm install postcss-preset-primer --save-dev ``` -------------------------------- ### Install Primer React dependencies Source: https://github.com/primer/react/blob/main/packages/react/README.md Commands to install the required packages using npm or Yarn. ```bash npm install -S @primer/react @primer/primitives ``` ```bash yarn add @primer/react @primer/primitives ``` -------------------------------- ### Install @primer/styled-react Source: https://github.com/primer/react/blob/main/packages/styled-react/README.md Use npm to add the package as a dependency to your project. ```bash npm install -S @primer/styled-react ``` -------------------------------- ### Install Primer React Source: https://github.com/primer/react/blob/main/README.md Use these commands to add the @primer/react package to your project dependencies. ```console npm install @primer/react ``` ```console yarn add @primer/react ``` -------------------------------- ### Install Dependencies in Docs Folder Source: https://github.com/primer/react/blob/main/contributor-docs/CONTRIBUTING.md Ensure dependencies are installed in both the root and the docs subfolder to avoid 'gatsby: command not found' errors. ```bash npm install ``` -------------------------------- ### Example plugin output Source: https://github.com/primer/react/blob/main/packages/rollup-plugin-import-css/README.md The resulting import structure after the plugin processes the CSS modules. ```ts // Example output import './path-to-header-[hash].css' export default { header: 'header___3s4s', } ``` -------------------------------- ### Start Storybook for Primer React Development Source: https://github.com/primer/react/blob/main/contributor-docs/CONTRIBUTING.md Use this command to launch the Storybook environment for developing and previewing Primer React components locally. Access it at http://localhost:6006/. ```sh npm start ``` -------------------------------- ### Install Playwright Dependencies Source: https://github.com/primer/react/blob/main/contributor-docs/testing.md Install Playwright and its browser dependencies. Required before running Playwright tests locally. ```bash npx playwright install --with-deps ``` -------------------------------- ### Basic vs. Responsive Value Example Source: https://github.com/primer/react/blob/main/contributor-docs/adrs/adr-018-responsive-values.md Illustrates the difference between a standard prop value and a responsive prop value in a component. ```tsx // Value // Responsive value ``` -------------------------------- ### Start Storybook for Accessibility Testing Source: https://github.com/primer/react/blob/main/contributor-docs/testing.md Command to launch Storybook with the environment variable required for accessibility verification. ```bash STORYBOOK=true npx start-storybook -p 6006 ``` -------------------------------- ### Usage Example of Responsive Padding in Stack Component Source: https://github.com/primer/react/blob/main/contributor-docs/authoring-css.md Demonstrates how to use the responsive padding prop by passing an object with different values for different viewport sizes. ```tsx // usage ``` -------------------------------- ### Node.js Package Exports Example Source: https://github.com/primer/react/blob/main/contributor-docs/adrs/adr-015-internal-modules.md An example from Node.js documentation showing how to define package exports, including a pattern to exclude internal files. ```json { "name": "my-package", "exports": { ".": "./lib/index.js", "./feature/*.js": "./feature/*.js", "./feature/internal/*": null } } ``` -------------------------------- ### Import and use a component Source: https://github.com/primer/react/blob/main/packages/react/README.md Basic example of importing a component from the package and rendering it in a React function. ```tsx import {Button} from '@primer/react' function App() { return } ``` -------------------------------- ### Implement Tabs with Utility Hooks Source: https://github.com/primer/react/blob/main/packages/react/src/experimental/Tabs/README.mdx Demonstrates how to use the `Tabs`, `useTabList`, `useTab`, and `useTabPanel` hooks to build a custom tabbed interface. This example shows the basic structure for creating tab buttons and their corresponding content panels. ```tsx import React from 'react' import {Tabs, useTabList, useTab, useTabPanel} from '@primer/react/experimental' function TabPanel({children, ...props}) { const tabPanelProps = useTabPanel(props) return
{children}
} function Tab({children, ...props}) { const tabProps = useTab(props) return } function TabList({children, ...props}) { const tabListProps = useTabList(props) return
{children}
} export function MyTabs() { return ( Tab 1 Tab 2

This is the content for Tab 1.

This is the content for Tab 2.

) } ``` -------------------------------- ### npm Package Exports Configuration (Example) Source: https://github.com/primer/react/blob/main/contributor-docs/adrs/adr-015-internal-modules.md This JSON configuration snippet illustrates how to define export patterns for an npm package, specifically showing how to include and exclude modules. ```json { exports: { // ... './lib-esm/*': { import: [ // ... ], require: [ // ... ], }, }, } ``` -------------------------------- ### Basic Primer React Component Structure Source: https://github.com/primer/react/blob/main/contributor-docs/CONTRIBUTING.md Example of a basic component file in Primer React, demonstrating imports, props, and component rendering. Ensure to include `SxProp` for styling capabilities. ```tsx import type React from 'react' import styles from './Component.module.css' import {clsx} from 'clsx' export type ComponentProps = { prop?: 'value1' | 'value2' className?: string } & SxProp const Component: React.FC> = ({ prop = 'value1', children, className, ...props }) => { return ( ) } Component.displayName = 'Component' export default Component ``` -------------------------------- ### Define props in .docs.json Source: https://github.com/primer/react/blob/main/packages/doc-gen/README.md Example of a manually defined property in a component's documentation file. ```json { "props": [ { "name": "alignContent", "type": "'start' | 'center'", "default": "start", "description": "Content alignment for when visuals are present.", "required": false } ] } ``` -------------------------------- ### Flash with custom styling and dismiss button Source: https://github.com/primer/react/blob/main/contributor-docs/adrs/adr-004-children-as-api.md Example of extending Flash functionality by using `sx` prop for custom layout and adding a dismiss button. ```jsx Changes saved! {isOpen && ( { console.log('Dialog closed via:', gesture) // 'close-button' or 'escape' setIsOpen(false) }} footerButtons={[ { content: 'Cancel', onClick: () => setIsOpen(false) }, { content: 'Confirm', buttonType: 'danger', onClick: () => { console.log('Confirmed!') setIsOpen(false) }, autoFocus: true } ]}>

Are you sure you want to delete this repository? All data will be permanently removed.

)} ) } ``` -------------------------------- ### Flash component with icon and explicit styling in children Source: https://github.com/primer/react/blob/main/contributor-docs/adrs/adr-004-children-as-api.md This example shows how developers can still pass an icon as a child with specific styling, but it requires more explicit configuration, highlighting the increased effort for non-standard usage. ```jsx Changes saved! ``` -------------------------------- ### Run v1.0.0-beta codemod Source: https://github.com/primer/react/blob/main/migrating.md Use this command to upgrade from 0.x.x-beta versions. ```bash npx jscodeshift -t node_modules/primer-react/codemods/v1.js path/to/src ``` -------------------------------- ### HTML button markup Source: https://github.com/primer/react/blob/main/contributor-docs/versioning.md Example of markup where whitespace between elements is significant for layout. ```html ``` -------------------------------- ### Implement ActionList with selection and grouping Source: https://context7.com/primer/react/llms.txt Shows how to build a list with multiple selection, dividers, group headings, and navigation links. ```tsx import {ActionList} from '@primer/react' import {FileIcon, FolderIcon, StarIcon, CheckIcon} from '@primer/octicons-react' function ActionListExample() { const [selected, setSelected] = React.useState([]) const toggleSelection = (item: string) => { setSelected(prev => prev.includes(item) ? prev.filter(i => i !== item) : [...prev, item] ) } return ( Recent files Documents toggleSelection('readme')} > README.md 12kb toggleSelection('package')} > package.json Configuration file Folders src {/* Link items navigate instead of triggering actions */} View starred items ) } ``` -------------------------------- ### Run v3.0.0-beta codemod Source: https://github.com/primer/react/blob/main/migrating.md Use this command to upgrade from v2.x.x-beta and update the package name to @primer/components. ```bash npx jscodeshift -t node_modules/@primer/components/codemods/v3.js path/to/src ``` -------------------------------- ### Inline Event Handler Usage Source: https://github.com/primer/react/blob/main/contributor-docs/versioning.md Example of an inline event handler where the type is inferred automatically. ```tsx function ExampleComponent() { return ( { // }} /> ) } ``` -------------------------------- ### Configure RootLayout with BaseStyles Source: https://github.com/primer/react/blob/main/packages/react/README.md Wrap the application in BaseStyles and import the necessary CSS theme files. ```tsx // Import each of the themes you would like to use, by default we are including // the light theme below import '@primer/primitives/dist/css/functional/themes/light.css' import {BaseStyles} from '@primer/react' function RootLayout() { return ( ) } ``` -------------------------------- ### ActionList Component Structure Source: https://github.com/primer/react/blob/main/contributor-docs/adrs/adr-004-children-as-api.md Example of using composite children within an ActionList to manage list items and their descriptions. ```jsx mona Monalisa Octocat primer-css GitHub ``` -------------------------------- ### Run v2.0.0-beta codemod Source: https://github.com/primer/react/blob/main/migrating.md Use this command to upgrade from v1.x.x-beta. Ensure component renames are handled in the specified order to avoid conflicts. ```bash npx jscodeshift -t node_modules/primer-react/codemods/v2.js path/to/src ``` -------------------------------- ### Increase component selector specificity Source: https://github.com/primer/react/blob/main/contributor-docs/adrs/adr-021-css-layers.md Example of a version update that breaks existing override styles due to increased specificity. ```css /* v1.1.0 of a component */ .Component[data-variant='default'] { color: var(--fgColor-default); } ``` -------------------------------- ### Migrating ActionList Group Title API Source: https://github.com/primer/react/blob/main/contributor-docs/adrs/adr-019-deprecating-props.md Example showing the transition from a deprecated title prop to the new ActionList.GroupHeading component. ```tsx // old API Item 1 Item 2 ``` ```tsx // new API Group title Item 1 Item 2 ``` -------------------------------- ### Configure postcss-preset-primer Source: https://github.com/primer/react/blob/main/packages/postcss-preset-primer/README.md Import and register the plugin within your postcss.config.js file. ```javascript // postcss.config.js const postcssPresetPrimer = require('postcss-preset-primer') /** @type {import('postcss-load-config').Config} */ const config = { plugins: [postcssPresetPrimer()], } module.exports = config ``` -------------------------------- ### Extracted Event Handler with Explicit Typing Source: https://github.com/primer/react/blob/main/contributor-docs/versioning.md Example of an extracted event handler function with an explicit React event type definition. ```tsx function ExampleComponent() { function onSelect(event: React.MouseEvent) { // } return } ``` -------------------------------- ### Implement Button variants and configurations Source: https://context7.com/primer/react/llms.txt Demonstrates various button styles, sizes, icon integration, loading states, and the IconButton component. ```tsx import {Button, IconButton} from '@primer/react' import {PlusIcon, SearchIcon, TrashIcon} from '@primer/octicons-react' function ButtonExamples() { return (
{/* Basic variants */} {/* With icons */} {/* Sizes */} {/* Loading state */} {/* Block (full width) */} {/* Icon-only button with tooltip */}
) } ``` -------------------------------- ### Project Structure with NPM Workspaces Source: https://github.com/primer/react/blob/main/contributor-docs/adrs/adr-014-npm-workspaces.md This illustrates the proposed directory layout for the primer/react project after implementing NPM workspaces. It shows the placement of the root package.json and various child workspaces. ```tree - root # Root package.json, defines workspaces - package.json # Workspaces - docs - packages - react - * - examples - nextjs - consumer-test - * # Top-level scripts - script # Testing & Development - .storybook - .playwright # Repo config - .github - .vscode ``` -------------------------------- ### Importing default and draft components Source: https://github.com/primer/react/blob/main/contributor-docs/adrs/adr-010-drafts-are-experimental.md Demonstrates how to import the default version of a component from the root of the package versus a new version from the /drafts directory. ```javascript // default version of component exported from root of package import {UnderlineNav} from '@primer/react' // new version of component exported from /drafts import {UnderlineNav} from '@primer/react/drafts' ``` -------------------------------- ### Configure ThemeProvider and useTheme Source: https://context7.com/primer/react/llms.txt Wraps the application to provide theming context and demonstrates accessing theme state with the useTheme hook. ```tsx import {ThemeProvider, BaseStyles, Button} from '@primer/react' function App() { return ( ) } // Using the useTheme hook to access theme context import {useTheme} from '@primer/react' function ThemeAwareComponent() { const {colorMode, setColorMode, resolvedColorScheme} = useTheme() return (

Current color scheme: {resolvedColorScheme}

) } ``` -------------------------------- ### Switching import path from drafts to experimental Source: https://github.com/primer/react/blob/main/contributor-docs/adrs/adr-010-drafts-are-experimental.md Illustrates the code change required to switch from importing a component via the /drafts path to the new /experimental path. ```diff // new version of component exported from /experimental - import { UnderlineNav } from '@primer/react/drafts' + import { UnderlineNav } from '@primer/react/experimental' ``` -------------------------------- ### Run v4.0.0-beta codemod Source: https://github.com/primer/react/blob/main/migrating.md Use this command to automatically upgrade component identifiers from v3.x.x-beta to v4.0.0-beta. ```bash npx jscodeshift -t node_modules/@primer/components/codemods/v4.js path/to/src ``` -------------------------------- ### Arrange elements with Stack Source: https://context7.com/primer/react/llms.txt Demonstrates horizontal, vertical, and responsive layouts using the Stack primitive with configurable gaps and alignment. ```tsx import {Stack, Button, Avatar, Text} from '@primer/react' function StackExamples() { return ( <> {/* Horizontal stack with gap */} octocat {/* Vertical stack */} Repository name A description of the repository {/* Responsive direction */} Main content area Sidebar {/* With wrap and justify */} ) } ``` -------------------------------- ### Run Unit Tests Source: https://github.com/primer/react/blob/main/contributor-docs/testing.md Execute all unit tests for the project. Use this to ensure code quality and catch regressions. ```bash npm test ``` -------------------------------- ### Unpreferred: Snapshot Test for Component Rendering Source: https://github.com/primer/react/blob/main/contributor-docs/adrs/adr-011-snapshot-tests.md This is an example of an unpreferred snapshot test for a React component. It captures the entire render tree, which can lead to large snapshots and difficulty in debugging changes. ```tsx it('renders correctly', () => { const tree = renderer.create(GitHub).toJSON() expect(tree).toMatchSnapshot() }) ``` -------------------------------- ### Dialog Component Behavior Hook Example Source: https://github.com/primer/react/blob/main/contributor-docs/behaviors.md Illustrates the expected return value structure for a component behavior hook like `usePopover`. The returned object contains props intended to be spread onto specific JSX elements. ```javascript { popoverProps: PropsObject, openClickTargetProps: PropsObject, closeClickTargetProps: PropsObject } ``` -------------------------------- ### Implement ActionMenu with items and controlled state Source: https://context7.com/primer/react/llms.txt Demonstrates standard ActionMenu usage with icons and descriptions, alongside a controlled state implementation. ```tsx import {ActionMenu, ActionList} from '@primer/react' import {GearIcon, CopyIcon, PencilIcon, TrashIcon} from '@primer/octicons-react' function ActionMenuExample() { const [selectedOption, setSelectedOption] = React.useState('option1') return ( Actions console.log('Copy clicked')}> Copy link console.log('Edit clicked')}> Edit Modify this item's content console.log('Delete clicked')}> Delete ) } // With controlled open state function ControlledActionMenu() { const [open, setOpen] = React.useState(false) return ( Menu ({open ? 'open' : 'closed'}) setOpen(false)}>Option 1 setOpen(false)}>Option 2 ) } ``` -------------------------------- ### Form Control with Input, Select, Textarea, and Checkbox - Primer React Source: https://context7.com/primer/react/llms.txt Utilize FormControl to structure form elements, providing labels, captions, and validation messages. This example demonstrates integrating TextInput with validation, a Select dropdown, a Textarea with a character limit hint, and a horizontally laid out Checkbox. ```tsx import {FormControl, TextInput, Select, Checkbox, Textarea} from '@primer/react' function FormExample() { const [username, setUsername] = React.useState('') const [hasError, setHasError] = React.useState(false) return (
{/* Text input with validation */} Username setUsername(e.target.value)} validationStatus={hasError ? 'error' : undefined} /> Enter your unique username {hasError && ( Username is already taken )} {/* Select dropdown */} Visibility {/* Textarea with character limit */} Description