Skip to content
P5-wrapperPublic

About

A NextJS specific library for the @P5-Wrapper/react project.

Topics

Resources

Code of conduct

Security policy

Stars

54 stars

Watchers

1 watching

Forks

Repository files navigation

@P5-wrapper/next

@P5-wrapper/next

A Next.js specific wrapper for @p5-wrapper/react, a component to integrate p5.js sketches into React apps.

Note:

This library simply re-exports the @p5-wrapper/react component as a Next.js dynamic component using next/dynamic with server-side rendering disabled. Nothing more.

For more in-depth information on the base component, check the documentation via the @p5-wrapper/react docs.

Features

  • NextReactP5Wrapper, a drop-in P5Canvas for Next.js apps
  • Client-side-only rendering via next/dynamic with ssr: false, because p5 manipulates the DOM and cannot run during server-side rendering
  • All P5Canvas props forwarded straight through to the underlying component
  • No runtime dependencies of its own, ESM and CJS builds with bundled types

Installation

@p5-wrapper/react, p5, next, react and react-dom are peer dependencies and must be installed in your project:

[npm|yarn|pnpm] [install|add] @p5-wrapper/next @p5-wrapper/react p5 next react react-dom

Usage

To use the component in your Next.js project, simply import it like so:

import { NextReactP5Wrapper } from "@p5-wrapper/next";
import { type Sketch } from "@p5-wrapper/react";
import React from "react";

const sketch: Sketch = (p5) => {
  p5.setup = () => p5.createCanvas(600, 400, p5.WEBGL);

  p5.draw = () => {
    p5.background(250);
    p5.normalMaterial();
    p5.push();
    p5.rotateZ(p5.frameCount * 0.01);
    p5.rotateX(p5.frameCount * 0.01);
    p5.rotateY(p5.frameCount * 0.01);
    p5.plane(100);
    p5.pop();
  };
};

export default function Page() {
  return <NextReactP5Wrapper sketch={sketch} />;
}

All props supported by P5Canvas can be passed straight through. NextReactP5Wrapper forwards everything to the underlying component. See the @p5-wrapper/react documentation for the full list of props, custom updaters, fallback UIs, and error and loading handling.

Why dynamic with ssr: false?

p5 sketches manipulate the DOM and browser APIs directly, which do not exist during server-side rendering. The wrapper therefore loads P5Canvas client side only, which is also what Next.js recommends for browser-only libraries.

Repository layout

./                         # repository root
├── .agents/               # agent configuration (DotAgents protocol)
├── .github/               # CI, CD, CodeQL workflows and the setup action
├── src/                   # the single-file library adapter
├── tests/                 # Vitest tests plus the setup bootstrap
├── .gitattributes         # git attributes
├── .gitignore             # git ignore rules
├── AGENTS.md              # agent-facing documentation
├── biome.json             # Biome configuration
├── CLAUDE.md              # symlink to AGENTS.md
├── LICENSE                # MIT licence
├── package.json           # pnpm manifest and scripts
├── pnpm-lock.yaml         # pnpm lockfile
├── pnpm-workspace.yaml    # pnpm workspace settings
├── README.md              # this file
├── tsconfig.json          # strict TypeScript config
├── tsconfig.node.json     # TypeScript config for the Vite config file
└── vite.config.ts         # library build and Vitest config

Development

Command Description
pnpm dev Run the Vite dev tooling
pnpm format Format all files with Biome
pnpm lint Check formatting and lint with Biome
pnpm test Run the Vitest suite
pnpm test:coverage Run tests with coverage
pnpm test:watch Watch mode
pnpm build Clean, type check, and build the library
pnpm integrate lint, test, and build in one command

The usual flow is to edit, run pnpm format, and check with pnpm lint. Run pnpm integrate as the final check at the end of a changeset, before opening a pull request, to catch any CI failures early. CI runs these checks automatically, but checking locally first saves a round trip. You will need Node.js and pnpm. The exact versions are pinned in package.json, so run pnpm install first.

Contributing

Before opening a pull request, make sure your changes pass every quality gate:

pnpm integrate

Pull requests are reviewed against the pull request template and must keep the public API in src/main.tsx backwards compatible. Commits follow the conventional commit style (feat:, fix:, chore:, etc.). Emoji prefixes are only used by automated pull requests.

The typescript dependency is pinned to the current major on purpose. The next major (Go-based) is not yet feature compatible with the JavaScript-based current API that the build toolchain builds on. Please do not bump past the pinned major or widen the pin in pull requests. We wait for full feature parity before migrating to the next major, no half measures.

AI-assisted contributions

Contributions developed with the assistance of an AI agent are welcome, but you, the developer, are always the ultimately responsible individual for your contributions. AI agents cannot author or certify them. When AI assistance is used:

  • Commit with --signoff (git commit -s) so that you certify the Developer Certificate of Origin for the work yourself
  • Optionally credit the agent with an Assisted-by: LLM trailer in the commit message
  • Ensure all work adheres to the MIT licence

Documentation

Agent-facing docs for this repo live in AGENTS.md and .agents/. The library source is in src/ and the tests in tests/.

Licence

This repository is licensed under the MIT licence.

About

A NextJS specific library for the @P5-Wrapper/react project.

Topics

Resources

Code of conduct

Security policy

Stars

54 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages