@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 +40 -18
- package/dist/react/NotionCustomBlock.js +1 -1
- package/dist/version.js +1 -1
- package/docs/block-location.md +2 -2
- package/docs/data-sources.md +5 -7
- package/docs/deployment.md +3 -1
- package/package.json +1 -1
- package/src/react/NotionCustomBlock.tsx +1 -1
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
|
|
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
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
`@notionhq/custom-blocks/react
|
|
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
|
|
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
|
|
27
|
-
serving
|
|
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
|
-
|
|
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
|
|
61
|
+
import { createRoot } from "react-dom/client";
|
|
56
62
|
import { App } from "./App";
|
|
63
|
+
import "./style.css";
|
|
57
64
|
|
|
58
|
-
|
|
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>`
|
|
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
|
|
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
|
|
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
|
|
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
|
-
|
|
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 `
|
|
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
package/docs/block-location.md
CHANGED
|
@@ -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
|
|
140
|
+
For React apps, `<NotionTokenScope>` applies both appearance values to the bundled Notion CSS variables automatically.
|
package/docs/data-sources.md
CHANGED
|
@@ -137,9 +137,10 @@ renderManifest(customBlock.getManifest());
|
|
|
137
137
|
|
|
138
138
|
## Example: querying a data source
|
|
139
139
|
|
|
140
|
-
|
|
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
|
|
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`.
|
package/docs/deployment.md
CHANGED
|
@@ -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
|
@@ -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}
|