Next.js
Installation
In your Next.js project, Install the Dopt React component you would like to use with your preferred JavaScript package manager. For example, if installing the modal component:
- npm
- Yarn
- pnpm
npm install @dopt/react-modal
yarn add @dopt/react-modal
pnpm add @dopt/react-modal
Setup
If you are leveraging Dopt components headlessly (i.e., using the hooks only), then you can skip this setup step as long as you are using the hooks exports (e.g., @dopt/react-*/hooks
). For example, if you are only using the modal component hooks:
import { useModal } from '@dopt/react-modal/hooks';
If you are leveraging Dopt React components as pre-built UI, then you'll need to complete an additional setup step to transpile the components before you can use them.
The following is only required if you are utilizing Dopt React components with the Pages Router. If you are using the components with the App Router, everything should work out of the box, but you'll need to utilize client components via 'use client'
at the top of your file.
Next.js 13
If you are using Next.js 13, add the Dopt React component package(s) to transpilePackages
your Next.js configuration. For example, if using the modal component:
/** @type {import('next').NextConfig} */
const nextConfig = {
transpilePackages: [
// 👇 Add the Dopt React components you are using
'@dopt/react-modal',
// 👇 These are dependencies that also need to be transpiled
'@dopt/react-theme',
'@dopt/react-rich-text',
'@dopt/core-theme',
'@dopt/core-rich-text',
],
}
module.exports = nextConfig
Next.js 12
If you are using Next.js 12, you will need to add the next-transpile-modules
plugin first.
- npm
- Yarn
- pnpm
npm install next-transpile-modules@9.1.0
yarn add next-transpile-modules@9.1.0
pnpm add next-transpile-modules@9.1.0
Then, you need to use the plugin and define the Dopt React component package(s) and their dependencies in your Next.js configuration. For example, if using the modal component:
const withTM = require('next-transpile-modules')(
[
// 👇 Add the Dopt React components you are using
'@dopt/react-modal',
// 👇 These are dependencies that also need to be transpiled
'@dopt/react-theme',
'@dopt/react-rich-text',
'@dopt/core-theme',
'@dopt/core-rich-text',
]
);
module.exports = withTM({});
Usage
Import the Dopt React component and use it as specified in the usage section of each component. For example, if using the modal component:
import Modal, { useModal } from '@dopt/react-modal';
function MyModal() {
const modal = useModal('my-flow.four-pandas-jam');
return (
<Modal.Root active={modal.active}>
<Modal.Overlay />
<Modal.Content>
<Modal.Header>
<Modal.Title>{modal.title}</Modal.Title>
<Modal.DismissIcon onClick={modal.dismiss} />
</Modal.Header>
<Modal.Body>{modal.body}</Modal.Body>
<Modal.Footer>
<Modal.DismissButton onClick={modal.dismiss}>
{modal.dismissLabel}
</Modal.DismissButton>
<Modal.CompleteButton onClick={modal.complete}>
{modal.completeLabel}
</Modal.CompleteButton>
</Modal.Footer>
</Modal.Content>
</Modal.Root>
);
}