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 namespace
Strings
Use single quotes for string literals, unless the string contains a single quote:') not single quotes (') in user-facing strings:
Objects
Use shorthand notation when possible:Optional Chaining
Optional chaining can be convenient but has pitfalls: Avoid when negating:React Components
Implement all components as function components using hooks: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
State Modifiers
Use modifier classes withis- prefix for state:
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:
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
- Review Testing Guide for testing conventions
- Check Architectural Decisions for design patterns
- See Common Pitfalls to avoid mistakes