Skip to main content
This document outlines coding guidelines specific to the Gutenberg project. Base coding guidelines follow the WordPress Coding Standards.

JavaScript

Language Features

Gutenberg uses modern JavaScript (ECMAScript) and JSX syntax, enabled through @wordpress/babel-preset-default. Important: Only language features that have reached Stage 4 (“Finished”) in the TC39 proposal process are supported.

Import Organization

Organize imports with multi-line comments separating different types:

Import Types

External dependencies: Third-party packages from npm
WordPress dependencies: Packages from the @wordpress namespace
Internal dependencies: Relative imports within your package

Strings

Use single quotes for string literals, unless the string contains a single quote:
Apostrophes: Use real apostrophes (') not single quotes (') in user-facing strings:
Template literals: Prefer template strings over concatenation:

Objects

Use shorthand notation when possible:

Optional Chaining

Optional chaining can be convenient but has pitfalls: Avoid when negating:
Be careful with boolean contexts:

React Components

Implement all components as function components using hooks:
Exception: Class components are acceptable only for error boundaries.

CSS

Class Naming Convention

Follow a BEM-inspired naming convention: Format: package-directory__descriptor-foo-bar
  • Root element: package-directory
  • Child elements: package-directory__descriptor
Example:

State Modifiers

Use modifier classes with is- prefix for state:
In CSS:

Component Isolation

A component’s class name should never be used outside its own folder (with rare exceptions).
  • Don’t inherit styles from other components
  • Render instances of other components instead
  • Duplicate styles if necessary to maintain isolation

SCSS File Naming for Blocks

style.scss: Loads on both frontend and editor
editor.scss: Loads only in the editor

TypeScript & JSDoc

Gutenberg uses TypeScript via JSDoc for type checking JavaScript files.

Custom Types

Define custom types with @typedef:
Naming: Use WP prefix and descriptive names without redundant suffixes.

Type Unions

Define sets of options as union types:

Importing Types

Use TypeScript import syntax in JSDoc:

Generic Types

Always provide details for generic types:

Documenting Components

Document component props using dot notation:

Examples in Documentation

Include usage examples with code blocks:

PHP

PHP_CodeSniffer

Gutenberg uses PHP_CodeSniffer with WordPress Coding Standards.

Running PHP Checks

Installing Locally

Private and Experimental APIs

Private APIs

Use the @wordpress/private-apis package for internal-only APIs:

Plugin-Only APIs

Export APIs only in the Gutenberg plugin:

Legacy Experimental APIs

Avoid using __experimental or __unstable prefixes for new APIs. Use private APIs or plugin-only APIs instead.

Code Quality Tools

ESLint

JavaScript linting with ESLint:

Prettier

Code formatting with Prettier:

TypeScript

Type checking via TypeScript:

Next Steps