@notionhq/custom-blocks 0.1.41 → 0.1.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -5,7 +5,12 @@
5
5
 
6
6
  SDK for building Notion custom blocks.
7
7
 
8
- A custom block runs as a sandboxed `<iframe>` inside the Notion app that has no direct access to the internet. The only channel between your block and Notion is a local `postMessage` bridge. This library implements the sandbox side of the bridge protocol and wraps it in a framework-neutral TypeScript API (`@notionhq/custom-blocks`) and typed React hooks (`@notionhq/custom-blocks/react`).
8
+ A custom block runs in a sandboxed `<iframe>` inside Notion. It has no direct
9
+ access to the internet. A local `postMessage` bridge connects the block to
10
+ Notion.
11
+
12
+ This package provides the sandbox side of the bridge. It exports a
13
+ framework-neutral TypeScript API and typed React hooks.
9
14
 
10
15
  ## Bundle tooling
11
16
 
@@ -14,17 +19,18 @@ Node build tools can import `build` and `upload` from
14
19
 
15
20
  ## Install
16
21
 
17
- Create a worker project with `ntn workers new --template custom` and use the dependencies and
18
- entrypoint it generates. In this repository, fixtures depend on the SDK through
19
- the workspace. React is optional unless the block imports
20
- `@notionhq/custom-blocks/react`. React blocks need `react` and `react-dom`.
22
+ Create a worker project with `ntn workers new --template custom`. Use the
23
+ dependencies and entrypoint that the Notion CLI generates.
24
+
25
+ React is optional. Blocks that import `@notionhq/custom-blocks/react` need
26
+ `react` and `react-dom`. This repository's fixtures use the workspace SDK.
21
27
 
22
- For local preview, run `ntn customblocks dev` from the worker project, then
23
- open `http://localhost:9873`. Install
28
+ For local preview, run `ntn workers customblocks dev` from the worker project.
29
+ Then open `http://localhost:9873`. Install
24
30
  [`@notionhq/custom-blocks-dev-shell`](https://www.npmjs.com/package/@notionhq/custom-blocks-dev-shell)
25
31
  as a devDependency to pin the shell and get its reference docs in
26
- `node_modules`; see [`docs/deployment.md`](./docs/deployment.md) for how local
27
- serving works.
32
+ `node_modules`. See [`docs/deployment.md`](./docs/deployment.md) for local
33
+ serving details.
28
34
 
29
35
  ## Quick start
30
36
 
@@ -43,7 +49,7 @@ worker.customBlock("hello", {
43
49
  });
44
50
  ```
45
51
 
46
- In the block's entry point defined above, wrap your React code in `<NotionCustomBlock>` so the SDK connects with Notion before anything renders:
52
+ Wrap your React app in `<NotionCustomBlock>` at the block's entry point:
47
53
 
48
54
  ```tsx
49
55
  // blocks/hello/src/index.tsx
@@ -52,10 +58,14 @@ import {
52
58
  NotionCustomBlock,
53
59
  NotionTokenScope,
54
60
  } from "@notionhq/custom-blocks/react";
55
- import ReactDOM from "react-dom/client";
61
+ import { createRoot } from "react-dom/client";
56
62
  import { App } from "./App";
63
+ import "./style.css";
57
64
 
58
- ReactDOM.createRoot(document.getElementById("root")!).render(
65
+ const root = document.getElementById("root");
66
+ if (!root) throw new Error("The root element is missing.");
67
+
68
+ createRoot(root).render(
59
69
  <NotionCustomBlock>
60
70
  <NotionTokenScope>
61
71
  <App />
@@ -76,6 +86,8 @@ export function App() {
76
86
  }
77
87
  ```
78
88
 
89
+ Save these styles in `blocks/hello/src/style.css`:
90
+
79
91
  ```css
80
92
  .app {
81
93
  color: var(--content-primary);
@@ -86,24 +98,34 @@ export function App() {
86
98
  }
87
99
  ```
88
100
 
89
- `<NotionCustomBlock>` runs the SDK host handshake (`connect` → `init` → `initResult`) and only mounts `children` once the host state is available. It measures the initial content before sending `initResult`, so the host can reveal the iframe at its correct height. Inside the wrapper, every hook returns non-nullable values — there's no separate gating component to write. It continues observing content height changes automatically.
101
+ `<NotionCustomBlock>` connects the SDK to the host and waits for host state before rendering its children.
102
+ It reports the initial content height so the host can size the iframe before displaying it.
103
+ It also reports later height changes automatically.
90
104
 
91
105
  ## Notion design tokens
92
106
 
93
- The optional `@notionhq/custom-blocks/nds.css` stylesheet provides the colors, spacing, typography, borders, and other design tokens used by Notion. Import it once, then put token-consuming UI inside `<NotionTokenScope>` as shown above.
107
+ The optional `@notionhq/custom-blocks/nds.css` stylesheet provides Notion CSS variables for matching Notion's UI.
108
+ These variables cover colors, spacing, typography, and borders.
109
+ Import the stylesheet once. Place components that use the variables inside `<NotionTokenScope>`, as shown above.
94
110
 
95
- `<NotionTokenScope>` applies the display and contrast modes selected by the host, so variables such as `--content-primary` and `--bg-base` stay in sync with Notion automatically. Hosts that do not provide a contrast mode use standard contrast.
111
+ `<NotionTokenScope>` applies the host's display and contrast modes to these variables.
112
+ Hosts that omit a contrast mode use standard contrast.
96
113
 
97
114
  `NotionTokenScopeProps` has one required `children` prop.
98
115
 
99
- The stylesheet is scoped rather than installed globally. Render portals inside `<NotionTokenScope>`, or wrap the portal container in another scope. Framework-neutral renderers can read the current appearance with `customBlock.getTheme()` and `customBlock.getContrastMode()`; see [Block Location & Appearance](./docs/block-location.md).
116
+ The CSS variables apply within the scope, not globally.
117
+ Place portal containers inside `<NotionTokenScope>`, or wrap the portal content in another `<NotionTokenScope>`.
118
+ Framework-neutral renderers can read the current appearance with `customBlock.getTheme()` and `customBlock.getContrastMode()`.
119
+ See [Block location and appearance](./docs/block-location.md).
100
120
 
101
121
  ## Reference
102
122
 
103
- API surface, one page per category. Import framework-neutral APIs from `@notionhq/custom-blocks`; import React hooks and components from `@notionhq/custom-blocks/react`. Hover docs in your editor cover the per-field detail; these pages cover usage shape and the gotchas.
123
+ Each page covers one API category. Import framework-neutral APIs from
124
+ `@notionhq/custom-blocks`. Import React hooks and components from
125
+ `@notionhq/custom-blocks/react`.
104
126
 
105
127
  - [`docs/lifecycle.md`](./docs/lifecycle.md) — `<NotionCustomBlock>`, `useCustomBlockInit`, `initCustomBlock`, `NotInIframeError`, `useCustomBlockAutoResize`. The handshake, the React wrapper, sizing.
106
- - [`docs/block-location.md`](./docs/block-location.md) — `useBlockId`, `useParent`, `usePage`, `useTheme`, and `useContrastMode`. Where the block sits and how to read the host's appearance.
128
+ - [`docs/block-location.md`](./docs/block-location.md) — `useBlockId`, `useParent`, `usePage`, `useTheme`, `useContrastMode`, `NotionTokenScope`, and `NotionTokenScopeProps`. Where the block sits and how to read the host's appearance.
107
129
  - [`docs/data-sources.md`](./docs/data-sources.md) — `useDataSource`, `useManifest`, `customBlock.getManifest`, the row, property, and date-value types, plus a worked example.
108
130
  - [`docs/pages.md`](./docs/pages.md) — `pages.create / get / update / delete`, parent variants (including the recommended `data_source_key`), property input shapes.
109
131
  - [`docs/users.md`](./docs/users.md) — `users.list / get`, the `NotionUser` shape, paging.
@@ -75,7 +75,7 @@ export function NotionCustomBlock({ children, timeoutMs, fallback = null, errorF
75
75
  if (host.status !== "initialized") {
76
76
  return _jsx(_Fragment, { children: fallback });
77
77
  }
78
- return (_jsxs(_Fragment, { children: [_jsx("div", { role: "status", className: "custom-blocks-standalone-banner", children: "Host not detected \u2014 running in standalone preview. Try `ntn customblocks dev` to run the block in a local development environment." }), children] }));
78
+ return (_jsxs(_Fragment, { children: [_jsx("div", { role: "status", className: "custom-blocks-standalone-banner", children: "Host not detected \u2014 running in standalone preview. Try `ntn workers customblocks dev` to run the block in a local development environment." }), children] }));
79
79
  }
80
80
  if (!init.isLoaded) {
81
81
  return _jsx(_Fragment, { children: fallback });
package/dist/version.js CHANGED
@@ -4,4 +4,4 @@
4
4
  *
5
5
  * WARNING: Generated during SDK publish. Do not edit in the published package.
6
6
  */
7
- export const CUSTOM_BLOCKS_SDK_VERSION = "0.1.41"
7
+ export const CUSTOM_BLOCKS_SDK_VERSION = "0.1.42"
@@ -118,7 +118,7 @@ const theme = customBlock.getTheme();
118
118
  ```
119
119
 
120
120
  ```tsx
121
- import { usePage, useTheme } from "@notionhq/custom-blocks";
121
+ import { usePage, useTheme } from "@notionhq/custom-blocks/react";
122
122
 
123
123
  export function Header() {
124
124
  const page = usePage();
@@ -137,4 +137,4 @@ function useContrastMode(): NotionContrastMode; // "standard" | "high"
137
137
 
138
138
  For non-React renderers, use `customBlock.getContrastMode()` after `initCustomBlock()` resolves.
139
139
 
140
- For React apps, `<NotionTokenScope>` applies both appearance values to the bundled Notion design tokens automatically. Setup and portal guidance live in the [package README](../README.md#notion-design-tokens).
140
+ For React apps, `<NotionTokenScope>` applies both appearance values to the bundled Notion CSS variables automatically.
@@ -137,9 +137,10 @@ renderManifest(customBlock.getManifest());
137
137
 
138
138
  ## Example: querying a data source
139
139
 
140
- A typical data-driven view picks a key, calls `useDataSource`, schema-checks the rows, and surfaces a setup hint when the mapped collection is missing the expected fields:
140
+ This example calls `useDataSource` with a declared key. It validates each row’s values before rendering:
141
141
 
142
142
  ```tsx
143
+ import { useState } from "react";
143
144
  import type { NotionDataSourcePage } from "@notionhq/custom-blocks";
144
145
  import { useDataSource } from "@notionhq/custom-blocks/react";
145
146
 
@@ -163,10 +164,7 @@ export function ScoreList() {
163
164
  const displayItems = items.filter(isComplete);
164
165
  if (displayItems.length === 0) {
165
166
  return (
166
- <div>
167
- Map a data source with key <code>{KEY}</code> exposing <code>name</code>{" "}
168
- (text) and <code>score</code> (number).
169
- </div>
167
+ <div>No rows have both a text name and a finite score.</div>
170
168
  );
171
169
  }
172
170
 
@@ -198,9 +196,9 @@ export function ScoreList() {
198
196
 
199
197
  ### Rows & values
200
198
 
201
- - `NotionDataSource` — a resolved data source: semantic key, `collectionSchema`, `propertyIdsByKey`, `propertySchemasById`.
199
+ - `NotionDataSource` — a resolved data source: semantic key, optional `collectionSchema`, `propertyIdsByKey`, and `propertySchemasById`.
202
200
  - `NotionDataSourcePage` — a single row exposed to app code: `{ id, propertiesById, propertiesByKey, update }`.
203
- - `NotionDataSourceValue` — the discriminated union of values that can appear inside `propertiesById[propertyId]`. Date values branch into the `NotionDateValue` union below.
201
+ - `NotionDataSourceValue` — the value union for `propertiesById` and `propertiesByKey`.
204
202
  - `NotionDataSourcePageUpdateArgs` / `UpdatePageResult` — arguments and result for the per-page `update` helper.
205
203
  - `NotionDataSourcePageUpdateInput` — deprecated alias for `NotionDataSourcePageUpdateArgs`.
206
204
  - `NotionDataSourcePageUpdateResult` — deprecated alias for `UpdatePageResult`.
@@ -7,9 +7,11 @@ A custom block manifest describes the data sources a block requires, including t
7
7
  - **Deployed** — deploying persists the manifest on the block's definition record in Notion. The host reads it from there and sends it to the SDK during initialization.
8
8
  - **Local development** — the dev shell extracts the same worker declaration at runtime and serves the selected block's manifest dynamically at `/manifest`.
9
9
 
10
+ Notion reconciles custom block definitions by their Worker capability keys during deployment.
11
+
10
12
  ## Local development
11
13
 
12
- Run `ntn customblocks dev` from a worker. It builds the worker, extracts its custom block declarations, starts one localhost Vite server per block, and renders them in the mock host at http://localhost:9873.
14
+ Run `ntn workers customblocks dev` from a worker. It builds the worker, extracts its custom block declarations, starts one localhost Vite server per block, and renders them in the mock host at http://localhost:9873.
13
15
 
14
16
  At startup, the SDK fetches the page-relative `manifest` path only when the bundle's hostname is exactly `localhost`, then forwards the response in `connect`. On every other hostname, the SDK skips this fetch and relies on the host-provided manifest from `init`.
15
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@notionhq/custom-blocks",
3
- "version": "0.1.41",
3
+ "version": "0.1.42",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -119,7 +119,7 @@ export function NotionCustomBlock({
119
119
  return (
120
120
  <>
121
121
  <div role="status" className="custom-blocks-standalone-banner">
122
- Host not detected — running in standalone preview. Try `ntn
122
+ Host not detected — running in standalone preview. Try `ntn workers
123
123
  customblocks dev` to run the block in a local development environment.
124
124
  </div>
125
125
  {children}