Skip to content

Code Components - Imports and assets

This page shows which imports work when writing Code Components, and where new import targets can be added.

The in-browser code editor is for editing component files that already exist in Canvas. It can use imports whose targets are already available, including imports that were pushed from a local codebase. It cannot add new local modules, static assets, SVG files, or package dependencies.

Use a local codebase when you need to add new files or dependencies.

Import typeIn-browser code editorAdd new files or dependencies from
Other Code Components with @/componentsYesIn-browser code editor or local codebase
Built-in package importsYesInstall in local codebase
Third-party package dependenciesYes, when already pushedLocal codebase
Shared local code with @/Yes, when already pushedLocal codebase
Static asset imports with ./ or @/Yes, when already pushedLocal codebase
SVG as a URLYes, when already pushedLocal codebase
SVG as a React component with ?reactYes, when already pushedLocal codebase
Relative JavaScript and TypeScript modulesNoNot supported
CSS side-effect importsNoNot supported
Font package importsNoNot supported

Use @/components to import another Code Component.

import Heading from '@/components/heading';

This path is the Code Component import namespace. In a local codebase, @/components/heading resolves to the heading component directly inside the configured componentDir. If componentDir is src/components, the local path is src/components/heading.

Local Code Component files must be inside the configured componentDir, and componentDir must be inside aliasBaseDir. Do not nest component folders inside other folders in componentDir.

// Do not use grouping folders in Code Component imports.
import Heading from '@/components/marketing/heading';

Use @/components/heading instead, and place the heading component directly inside the configured componentDir.

Use built-in package imports.

import { clsx } from 'clsx';

Built-in packages are already available in the in-browser code editor. In a local codebase, install the package in your project so local builds can resolve it.

See Built-in packages for available packages and examples.

Use a local codebase to add packages that are not built in. Install the package with your package manager, import it with a standard package import, and push the component with the Canvas CLI.

Terminal window
npm install date-fns
import { format } from 'date-fns';
export default function EventDate({ date }) {
return <time>{format(new Date(date), 'PPP')}</time>;
}

When you run npx canvas push, the CLI builds the dependency with the component. The import continues to work when the pushed component is edited in the in-browser code editor.

Use packages that can run in the browser.

Use @/ to import shared local code. In a default project, @/ points to the src directory.

import { formatDate } from '@/lib/formatDate';

Use relative imports for static assets that live next to a component.

import heroImage from './hero.webp';
export default function Hero() {
return <img src={heroImage} alt="" />;
}

Static assets can also be imported with @/.

import introVideo from '@/assets/intro.mp4';

Supported static asset types include images, SVG files, audio, video, and font files.

Import an SVG without a query string when you need a URL. This works like other static asset imports.

import logoUrl from './logo.svg';
export default function LogoImage() {
return <img src={logoUrl} alt="Drupal Canvas" />;
}

Import an SVG with ?react when you need a React component. This uses SVGR during the build.

import LogoIcon from './logo.svg?react';
export default function Logo() {
return <LogoIcon aria-hidden className="size-6" />;
}

For TypeScript projects, add the SVGR client types to a Vite environment file.

/// <reference types="vite/client" />
/// <reference types="vite-plugin-svgr/client" />

The ?react form is component code, not a static asset URL. Use plain .svg imports when you want Canvas to treat the SVG as a file asset.

JavaScript and TypeScript modules inside component directories

Section titled “JavaScript and TypeScript modules inside component directories”

The @/components path is for importing other Code Components and component-local static assets. Do not use it to import helper files from inside a component directory.

// Do not use this.
import { formatPrice } from '@/components/pricing-card/helpers';

Move shared helpers to a directory outside @/components.

import { formatPrice } from '@/lib/formatPrice';

Relative JavaScript and TypeScript modules

Section titled “Relative JavaScript and TypeScript modules”

Relative imports are supported for static assets, but not for JavaScript and TypeScript modules that should be shared across components.

// Do not use this.
import { formatDate } from './formatDate';

Use an @/ import instead.

import { formatDate } from '@/lib/formatDate';

CSS side-effect imports are not supported in component JavaScript or TypeScript files.

// Do not use these.
import 'swiper/css';
import '@/lib/styles/carousel.css';

Put component styles in the component CSS file. Put shared global styles in the project global CSS file.

Font package imports are not supported in component JavaScript or TypeScript files.

// Do not use this.
import '@fontsource/inter';

Use Brand Kit fonts for fonts that should be available in Canvas.