npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@atomicsmash/blocks-helpers

v4.1.0

Published

This package provides types which make build native WordPress blocks much easier. It is opinionated to our preferred way of writing blocks, and may not cover every use case or implementation.

Downloads

203

Readme

WordPress Blocks Helpers

This package provides types which make build native WordPress blocks much easier. It is opinionated to our preferred way of writing blocks, and may not cover every use case or implementation.

Main types and how to use them

registerBlockType

This is the only piece of JS code in this package, and it is only used so the parameter types for the function are accurate. Simply replace the function imported from WP with this function.

Simple usage:

import type { Attributes } from "./versions/v1";
// Instead of import { registerBlockType } from "@wordpress/blocks";
import { registerBlockType } from "@atomicsmash/blocks-helpers";
import blockMetaData from "./block.json";
import { v1 } from "./versions/v1";

/**
 * Every block starts by registering a new block type definition.
 *
 * @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/
 */
registerBlockType<Attributes>(blockMetaData.name, {
	...v1,
});

Full example include block deprecation:

import type { InterpretedAttributes as v1InterpretedAttributes } from "./versions/v1";
import type {
	Attributes,
	InterpretedAttributes as v2InterpretedAttributes,
} from "./versions/v2";
import { registerBlockType } from "@atomicsmash/blocks-helpers";
import blockMetaData from "./block.json";
import { v1 } from "./versions/v1";
import { v2 } from "./versions/v2";

type AllPossibleAttributes = v1InterpretedAttributes & v2InterpretedAttributes;

registerBlockType<Attributes, v2InterpretedAttributes, AllPossibleAttributes>(
	blockMetaData.name,
	{
		...v2,
		deprecated: [v1],
	},
);

Attributes

import type {
	BlockAttributes,
	InterpretAttributes,
} from "@atomicsmash/blocks-helpers";
/**
 * Allows you to write block attributes with autocomplete and validation.
 * Prefer satisfies over type annotations for type narrowing.
 */
export const attributes = {
	someAttribute: {
		type: "string",
	},
} as const satisfies BlockAttributes;
export type Attributes = typeof attributes;
/**
 * This type helper interprets your attributes into what WordPress will
 * provide them to you as in your Edit / Save components.
 */
export type InterpretedAttributes = InterpretAttributes<Attributes>;

Supports

import type { BlockSupports } from "@atomicsmash/blocks-helpers";
/**
 * Allows you to write block supports with autocomplete and validation.
 * Prefer satisfies over type annotations for type narrowing.
 */
export const supports = {
	anchor: true,
} as const satisfies BlockSupports;
export type Supports = typeof supports;

Providing Context

import type {
	BlockProvidesContext,
	InterpretContext,
} from "@atomicsmash/blocks-helpers";
/**
 * Allows you to write block provides context with autocomplete and validation.
 * Prefer satisfies over type annotations for type narrowing.
 *
 * `Attributes` here is the same type as the attributes for the block as you
 * cannot provide context for anything except the attributes of your block.
 */
export const providesContext = {
	"namespace/someContextValue": "someAttribute",
} as const satisfies BlockProvidesContext<Attributes>;
export type ProvidesContext = typeof providesContext;
/**
 * This type helper interprets your context into what WordPress will
 * provide them to you as in your Edit component.
 * This is most commonly used to provide information to UsesContext.
 */
export type InterpretedContext = InterpretContext<Attributes, ProvidesContext>;

Using Context

import type {
	BlockUsesContext,
	InterpretUsedContext,
} from "@atomicsmash/blocks-helpers";
import type { InterpretedContext as InterpretedContext1 } from "some-block";
import type { InterpretedContext as InterpretedContext2 } from "some-other-block";
/**
 * Allows you to write block uses context with autocomplete and validation.
 * Prefer satisfies over type annotations for type narrowing.
 *
 * InterpretedContext here is the type exported by the block providing context.
 * You can combine multiple types if using context from multiple blocks, it
 * will only output the types of the context you're actually using.
 */
type InterpretedContext = InterpretedContext1 & InterpretedContext2;
export const usesContext = [
	"namespace/someContextValue",
	"namespace/someContextValue2",
] as const satisfies BlockUsesContext<InterpretedContext>;
export type UsesContext = typeof usesContext;
export type InterpretedUsedContext = InterpretUsedContext<
	UsesContext,
	InterpretedContext
>;

BlockEditProps

This type helper uses the types you created in the block definition to add prop types to your Edit function.

import type { InterpretedAttributes, InterpretedUsedContext } from "./index";
import type { BlockEditProps } from "@atomicsmash/blocks-helpers";
import type { Element } from "@wordpress/element";

export function Edit({
	attributes,
	setAttributes,
	context,
}: BlockEditProps<InterpretedAttributes, InterpretedUsedContext>): Element {
	const { someAttribute } = attributes;
	setAttributes({ someAttribute: "Template block" });
	return (
		<>
			Attribute value: {someAttribute}
			Context value: {context["namespace/someContextValue"]}
		</>
	);
}

BlockSaveProps

This type helper uses the types you created in the block definition to add prop types to your Save function.

import type { InterpretedAttributes } from "./index";
import type { BlockSaveProps } from "@atomicsmash/blocks-helpers";
import type { Element } from "@wordpress/element";

export function Save({
	attributes,
}: BlockSaveProps<InterpretedAttributes>): Element {
	const { someAttribute } = attributes;
	return <>{someAttribute}</>;
}

BlockDefinition

This is the combined type of the block definition so you can type check a single block version definition (with migrated blocks, you may have more that one definition for the same block)

import type { CurrentBlockDefinition } from "@atomicsmash/blocks-helpers";

export const v2 = {
	attributes,
	supports,
	edit: Edit,
	save: Save,
} satisfies CurrentBlockDefinition<Attributes, InterpretedAttributes>;

Block deprecations

At some point you will need to update the version of a block to a new version. The DeprecatedBlock helper can help you define the block, while BlockMigrateDeprecationFunction and BlockIsDeprecationEligibleFunction can help you with the migrate and isEligible functions respectively.

import type { InterpretedAttributes as NewInterpretedAttributes } from "../v2/index";

// v1 attributes (InterpretedAttributes) defined here

const migrate: BlockMigrateDeprecationFunction<
	InterpretedAttributes,
	NewInterpretedAttributes
> = (attributes, innerBlocks) => {
	return []; // Your migration code here
};

const isEligible: BlockIsDeprecationEligibleFunction<InterpretedAttributes> = (
	attributes,
	innerBlocks,
) => {
	return true; // Is Eligible logic here
};

export const v1 = {
	attributes,
	supports,
	migrate,
	isEligible,
	save: Save,
	// note there is no Edit as this is not used.
} satisfies DeprecatedBlock<InterpretedAttributes>;

Beta/untested types

  • Block transforms
  • Block examples