@notionhq/custom-blocks 0.2.11 → 0.2.12
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 +73 -69
- package/bin/notion-custom-blocks/cli.js +0 -0
- package/dist/version.js +1 -1
- package/docs/block-location.md +71 -45
- package/docs/data-sources.md +242 -141
- package/docs/deployment.md +30 -20
- package/docs/errors.md +33 -27
- package/docs/lifecycle.md +82 -72
- package/docs/pages.md +137 -81
- package/docs/users.md +65 -51
- package/package.json +65 -65
package/docs/deployment.md
CHANGED
|
@@ -1,19 +1,30 @@
|
|
|
1
1
|
# Deployment and manifests
|
|
2
2
|
|
|
3
|
-
A custom block is a static web bundle — an `index.html` plus optional assets — that Notion loads
|
|
3
|
+
A custom block is a static web bundle — an `index.html` plus optional assets — that Notion loads
|
|
4
|
+
into a sandboxed `<iframe>`. Custom blocks ship as part of a worker: declare the block source and
|
|
5
|
+
data source schema with `worker.customBlock(...)` in the worker's `src/index.ts`, deploy with
|
|
6
|
+
`ntn workers deploy`, and Notion serves the built bundle.
|
|
4
7
|
|
|
5
|
-
A custom block manifest describes the data sources a block requires, including their semantic keys,
|
|
8
|
+
A custom block manifest describes the data sources a block requires, including their semantic keys,
|
|
9
|
+
display metadata, and property schemas. The block's `worker.customBlock(...)` declaration is the
|
|
10
|
+
manifest's single source of truth:
|
|
6
11
|
|
|
7
|
-
- **Deployed** — deploying persists the manifest on the block's definition record in Notion. The
|
|
8
|
-
|
|
12
|
+
- **Deployed** — deploying persists the manifest on the block's definition record in Notion. The
|
|
13
|
+
host reads it from there and sends it to the SDK during initialization.
|
|
14
|
+
- **Local development** — the dev shell extracts the same worker declaration at runtime and serves
|
|
15
|
+
the selected block's manifest dynamically at `/manifest`.
|
|
9
16
|
|
|
10
17
|
Notion reconciles custom block definitions by their Worker capability keys during deployment.
|
|
11
18
|
|
|
12
19
|
## Local development
|
|
13
20
|
|
|
14
|
-
Run `ntn workers customblocks dev` from a worker. It builds the worker, extracts its custom block
|
|
21
|
+
Run `ntn workers customblocks dev` from a worker. It builds the worker, extracts its custom block
|
|
22
|
+
declarations, starts one localhost Vite server per block, and renders them in the mock host at
|
|
23
|
+
http://localhost:9873.
|
|
15
24
|
|
|
16
|
-
At startup, the SDK fetches the page-relative `manifest` path only when the bundle's hostname is
|
|
25
|
+
At startup, the SDK fetches the page-relative `manifest` path only when the bundle's hostname is
|
|
26
|
+
exactly `localhost`, then forwards the response in `connect`. On every other hostname, the SDK skips
|
|
27
|
+
this fetch and relies on the host-provided manifest from `init`.
|
|
17
28
|
|
|
18
29
|
## Types
|
|
19
30
|
|
|
@@ -24,10 +35,10 @@ At startup, the SDK fetches the page-relative `manifest` path only when the bund
|
|
|
24
35
|
|
|
25
36
|
## Apps SDK integration
|
|
26
37
|
|
|
27
|
-
`notion-custom-blocks build --manifest dist/manifest.json --out <dir>` reads
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
38
|
+
`notion-custom-blocks build --manifest dist/manifest.json --out <dir>` reads static Apps SDK
|
|
39
|
+
capabilities. Paths in the manifest stay relative to the project root (the current directory, or
|
|
40
|
+
`--cwd`). Without `--manifest`, the command continues to import the Workers SDK bundle at
|
|
41
|
+
`dist/index.js`.
|
|
31
42
|
|
|
32
43
|
Node build tools use one entrypoint:
|
|
33
44
|
|
|
@@ -35,15 +46,14 @@ Node build tools use one entrypoint:
|
|
|
35
46
|
import { build, upload } from "@notionhq/custom-blocks/bundle"
|
|
36
47
|
```
|
|
37
48
|
|
|
38
|
-
`build` accepts `BuildArgs` and returns `BuildOutput` containing `BuiltBlock`
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
these functions from `build-blocks` and `upload-blocks`.
|
|
49
|
+
`build` accepts `BuildArgs` and returns `BuildOutput` containing `BuiltBlock` results. `upload`
|
|
50
|
+
accepts `UploadsInput`, a map of block keys to `UploadTarget`, and returns `UploadOutput`.
|
|
51
|
+
`printBuildOutput` writes the sentinel-wrapped build result; `parseUploadsInput` validates JSON
|
|
52
|
+
upload arguments. The Apps CLI calls these functions from `build-blocks` and `upload-blocks`.
|
|
43
53
|
|
|
44
|
-
Relative `--out` paths also resolve from `--cwd`. Build results include
|
|
45
|
-
|
|
54
|
+
Relative `--out` paths also resolve from `--cwd`. Build results include `bundleSizeBytes`, the exact
|
|
55
|
+
compressed tarball length, for size-bound uploads.
|
|
46
56
|
|
|
47
|
-
Upload targets may include a `headers` map containing the headers returned by
|
|
48
|
-
|
|
49
|
-
|
|
57
|
+
Upload targets may include a `headers` map containing the headers returned by the server presign,
|
|
58
|
+
including `Content-Length` and object tags. The uploader forwards these alongside the tarball
|
|
59
|
+
checksum and content type.
|
package/docs/errors.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
# Error handling
|
|
2
2
|
|
|
3
|
-
Request-style SDK APIs resolve to a result with `status: "success"` or `status: "error"`.
|
|
3
|
+
Request-style SDK APIs resolve to a result with `status: "success"` or `status: "error"`.
|
|
4
|
+
Conceptually, success payloads vary by API while error results contain an `error` object with a
|
|
5
|
+
consistent structure:
|
|
4
6
|
|
|
5
7
|
```ts
|
|
6
|
-
type Result<T> =
|
|
7
|
-
| { status: "success"; value: T }
|
|
8
|
-
| { status: "error"; error: CustomBlockErrorInfo };
|
|
8
|
+
type Result<T> = { status: "success"; value: T } | { status: "error"; error: CustomBlockErrorInfo }
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
## Error format
|
|
@@ -14,53 +14,59 @@ SDK errors use `CustomBlockErrorInfo`:
|
|
|
14
14
|
|
|
15
15
|
```ts
|
|
16
16
|
type CustomBlockErrorInfo<TCode extends string = string> = {
|
|
17
|
-
code: TCode
|
|
18
|
-
message: string
|
|
19
|
-
isRetryable: boolean
|
|
20
|
-
}
|
|
17
|
+
code: TCode
|
|
18
|
+
message: string
|
|
19
|
+
isRetryable: boolean
|
|
20
|
+
}
|
|
21
21
|
```
|
|
22
22
|
|
|
23
23
|
- `code` is a stable identifier intended for application logic.
|
|
24
24
|
- `message` provides developer-facing details and is not a stable identifier.
|
|
25
25
|
- `isRetryable` indicates whether retrying the operation could plausibly succeed.
|
|
26
26
|
|
|
27
|
-
Each API narrows `code` to its own open string union. The known literals provide autocomplete, but
|
|
27
|
+
Each API narrows `code` to its own open string union. The known literals provide autocomplete, but
|
|
28
|
+
newer hosts may send codes that an older SDK does not list.
|
|
28
29
|
|
|
29
30
|
## API-specific error types
|
|
30
31
|
|
|
31
|
-
- Pages: `CustomBlockCreatePageErrorCode` / `CustomBlockCreatePageErrorInfo`,
|
|
32
|
-
|
|
32
|
+
- Pages: `CustomBlockCreatePageErrorCode` / `CustomBlockCreatePageErrorInfo`,
|
|
33
|
+
`CustomBlockGetPageErrorCode` / `CustomBlockGetPageErrorInfo`, `CustomBlockOpenPageErrorCode` /
|
|
34
|
+
`CustomBlockOpenPageErrorInfo`, and `CustomBlockUpdatePageErrorCode` /
|
|
35
|
+
`CustomBlockUpdatePageErrorInfo`.
|
|
36
|
+
- Users: `CustomBlockGetUserErrorCode` / `CustomBlockGetUserErrorInfo` and
|
|
37
|
+
`CustomBlockListUsersErrorCode` / `CustomBlockListUsersErrorInfo`.
|
|
33
38
|
- Data sources: `CustomBlockQueryDataSourceErrorCode` / `CustomBlockQueryDataSourceErrorInfo`.
|
|
34
|
-
- Shared resolution failures: `CustomBlockPropertyErrorCode` / `CustomBlockPropertyErrorInfo` and
|
|
39
|
+
- Shared resolution failures: `CustomBlockPropertyErrorCode` / `CustomBlockPropertyErrorInfo` and
|
|
40
|
+
`CustomBlockDataSourceResolutionErrorCode` / `CustomBlockDataSourceResolutionErrorInfo`.
|
|
35
41
|
|
|
36
42
|
## Initialization errors
|
|
37
43
|
|
|
38
|
-
Unlike request helpers, `initCustomBlock` rejects with `CustomBlockInitializationError`. Its `code`
|
|
44
|
+
Unlike request helpers, `initCustomBlock` rejects with `CustomBlockInitializationError`. Its `code`
|
|
45
|
+
type is `CustomBlockInitializationErrorCode`, which covers failures reported by either side of the
|
|
46
|
+
handshake plus SDK-local iframe errors. For example, hosts can report `init_timeout` when Notion did
|
|
47
|
+
not finish loading host dependencies in time.
|
|
39
48
|
|
|
40
49
|
`CustomBlockInitializationErrorInfo` is the matching `{ code, message, isRetryable }` object type.
|
|
41
50
|
|
|
42
51
|
Initialization errors have direction-specific meanings:
|
|
43
52
|
|
|
44
|
-
- `invalid_connect_payload` means that the host could not parse the SDK's
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
- `
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
- `invalid_init_result_payload` means that the host could not parse the SDK's
|
|
54
|
-
`initResult`. This is a host-local error. The SDK does not send this code.
|
|
55
|
-
- `no_init_result` means that the host did not receive a valid `initResult`
|
|
56
|
-
before its timeout.
|
|
53
|
+
- `invalid_connect_payload` means that the host could not parse the SDK's `connect` message. The
|
|
54
|
+
host sends this code in `init.error` when it can read the `initializationId`.
|
|
55
|
+
- `invalid_init_payload` means that the SDK could not parse the host's `init` message. The SDK sends
|
|
56
|
+
this code in `initResult.error` when it can read the expected `initializationId`.
|
|
57
|
+
- `invalid_init_bindings` means that the SDK could not apply the data source bindings in the host's
|
|
58
|
+
valid `init` message. The SDK sends this code in `initResult.error`.
|
|
59
|
+
- `invalid_init_result_payload` means that the host could not parse the SDK's `initResult`. This is
|
|
60
|
+
a host-local error. The SDK does not send this code.
|
|
61
|
+
- `no_init_result` means that the host did not receive a valid `initResult` before its timeout.
|
|
57
62
|
|
|
58
63
|
See [Lifecycle and initialization](./lifecycle.md) for initialization-specific error codes.
|
|
59
64
|
|
|
60
65
|
## Handling errors
|
|
61
66
|
|
|
62
67
|
- Branch on `code` and `isRetryable`, never on `message`.
|
|
63
|
-
- Keep a default branch when switching on `code`. The known literals provide autocomplete, but newer
|
|
68
|
+
- Keep a default branch when switching on `code`. The known literals provide autocomplete, but newer
|
|
69
|
+
hosts may send codes that an older SDK does not list.
|
|
64
70
|
- Treat `isRetryable` as advisory when deciding whether to offer or schedule a retry.
|
|
65
71
|
- Use `message` for developer-facing details, not as a stable identifier.
|
|
66
72
|
- `"unknown_error"` means the host encountered an unexpected or unclassified failure.
|
package/docs/lifecycle.md
CHANGED
|
@@ -1,66 +1,58 @@
|
|
|
1
1
|
# Lifecycle
|
|
2
2
|
|
|
3
|
-
The SDK and host use a three-message `postMessage` handshake to initialize
|
|
4
|
-
a custom block.
|
|
3
|
+
The SDK and host use a three-message `postMessage` handshake to initialize a custom block.
|
|
5
4
|
|
|
6
5
|
This handshake can be completed with one of these APIs:
|
|
7
6
|
|
|
8
|
-
- `<NotionCustomBlock>` for most React blocks. It starts initialization and
|
|
9
|
-
|
|
10
|
-
- `useCustomBlockInit()` for React blocks that need to control loading and
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
initialization before it renders.
|
|
7
|
+
- `<NotionCustomBlock>` for most React blocks. It starts initialization and renders `children` after
|
|
8
|
+
the host state is ready.
|
|
9
|
+
- `useCustomBlockInit()` for React blocks that need to control loading and error states.
|
|
10
|
+
- `initCustomBlock()` for framework-neutral code or code that must await initialization before it
|
|
11
|
+
renders.
|
|
14
12
|
|
|
15
|
-
Custom bridge integrations can also handle the `postMessage` wire messages
|
|
16
|
-
|
|
13
|
+
Custom bridge integrations can also handle the `postMessage` wire messages directly. This is
|
|
14
|
+
advanced usage for host or integration authors.
|
|
17
15
|
|
|
18
16
|
## Initialization protocol message
|
|
19
17
|
|
|
20
18
|
The full `postMessage` handshake is:
|
|
21
19
|
|
|
22
|
-
1. The SDK sends `connect` with a new `initializationId` and version
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
`initResult` with the same `initializationId` and the initial block height.
|
|
20
|
+
1. The SDK sends `connect` with a new `initializationId` and version information.
|
|
21
|
+
2. The host sends `init` with the same `initializationId`, the manifest, data source bindings, and
|
|
22
|
+
additional block and app context.
|
|
23
|
+
3. The SDK validates and applies `init`, renders the initial content, and sends `initResult` with
|
|
24
|
+
the same `initializationId` and the initial block height.
|
|
28
25
|
|
|
29
|
-
Both sides must check that the `initializationId` matches. The SDK ignores an
|
|
30
|
-
|
|
31
|
-
matching `init`.
|
|
26
|
+
Both sides must check that the `initializationId` matches. The SDK ignores an `init` with a
|
|
27
|
+
different ID. It sends `initResult` only after it applies a matching `init`.
|
|
32
28
|
|
|
33
|
-
The initialization promise resolves after the SDK applies the host state.
|
|
34
|
-
|
|
35
|
-
`initResult` after the initial render.
|
|
29
|
+
The initialization promise resolves after the SDK applies the host state. Await it before rendering
|
|
30
|
+
code that reads initialized state. The SDK sends `initResult` after the initial render.
|
|
36
31
|
|
|
37
|
-
- The SDK waits for the host to respond with `init` or `init.error`, without
|
|
38
|
-
|
|
39
|
-
|
|
32
|
+
- The SDK waits for the host to respond with `init` or `init.error`, without an SDK-local timeout.
|
|
33
|
+
Host-owned timeout errors such as `init_timeout` reject the promise with
|
|
34
|
+
`CustomBlockInitializationError`.
|
|
40
35
|
- If a matching `init` is malformed, the SDK sends `initResult.error` with code
|
|
41
36
|
`invalid_init_payload` and rejects with the same structured error.
|
|
42
|
-
- If the host sends incomplete or invalid data source bindings, the SDK sends
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
- After initialization, compatible hosts use `*Changed` messages to push live
|
|
48
|
-
updates.
|
|
37
|
+
- If the host sends incomplete or invalid data source bindings, the SDK sends `initResult.error`
|
|
38
|
+
with code `invalid_init_bindings` and rejects with the same structured error.
|
|
39
|
+
- If the SDK cannot correlate a malformed `init` to the handshake, it sends `invalidHostMessage`
|
|
40
|
+
when safe.
|
|
41
|
+
- After initialization, compatible hosts use `*Changed` messages to push live updates.
|
|
49
42
|
- `initCustomBlock()` is idempotent. Later calls return the same promise.
|
|
50
43
|
|
|
51
44
|
## Rendering inside a host
|
|
52
45
|
|
|
53
|
-
The SDK expects the custom block to run inside an iframe. In a top-level
|
|
54
|
-
|
|
55
|
-
`not_in_iframe`.
|
|
46
|
+
The SDK expects the custom block to run inside an iframe. In a top-level browser tab, initialization
|
|
47
|
+
rejects with `NotInIframeError` code `not_in_iframe`.
|
|
56
48
|
|
|
57
|
-
`<NotionCustomBlock>` catches this error and renders a standalone preview with
|
|
58
|
-
|
|
49
|
+
`<NotionCustomBlock>` catches this error and renders a standalone preview with a warning. This
|
|
50
|
+
supports local development when no host is available.
|
|
59
51
|
|
|
60
52
|
## Sizing
|
|
61
53
|
|
|
62
|
-
Custom blocks resize to fit their content by default. To set a shorter height, set
|
|
63
|
-
`
|
|
54
|
+
Custom blocks resize to fit their content by default. To set a shorter height, set `max-height` and
|
|
55
|
+
`overflow-y` on `#root`:
|
|
64
56
|
|
|
65
57
|
```css
|
|
66
58
|
#root {
|
|
@@ -71,64 +63,67 @@ Custom blocks resize to fit their content by default. To set a shorter height, s
|
|
|
71
63
|
|
|
72
64
|
The Notion app enforces a maximum custom block height of 10,000 pixels.
|
|
73
65
|
|
|
74
|
-
The SDK measures the `#root` element and sends its initial height in
|
|
75
|
-
|
|
76
|
-
|
|
66
|
+
The SDK measures the `#root` element and sends its initial height in `initResult.success`. It sends
|
|
67
|
+
later height changes in `resize` messages. The host uses these messages to keep the iframe height in
|
|
68
|
+
sync.
|
|
77
69
|
|
|
78
70
|
Using `<NotionCustomBlock>` enables this behavior automatically.
|
|
79
71
|
|
|
80
72
|
## API
|
|
81
73
|
|
|
82
|
-
Import framework-neutral APIs from `@notionhq/custom-blocks`. Import React
|
|
83
|
-
|
|
74
|
+
Import framework-neutral APIs from `@notionhq/custom-blocks`. Import React hooks and components from
|
|
75
|
+
`@notionhq/custom-blocks/react`.
|
|
84
76
|
|
|
85
|
-
The APIs below require successful initialization unless stated otherwise.
|
|
86
|
-
|
|
87
|
-
`
|
|
88
|
-
`isLoaded` gate of `useCustomBlockInit`, single-value hooks return non-nullable
|
|
89
|
-
values.
|
|
77
|
+
The APIs below require successful initialization unless stated otherwise. Initialized-only hooks and
|
|
78
|
+
`customBlock` getters throw before `initCustomBlock()` resolves. Inside `<NotionCustomBlock>`, or
|
|
79
|
+
after the `isLoaded` gate of `useCustomBlockInit`, single-value hooks return non-nullable values.
|
|
90
80
|
|
|
91
81
|
### `<NotionCustomBlock>`
|
|
92
82
|
|
|
93
83
|
```ts
|
|
94
84
|
type NotionCustomBlockProps = {
|
|
95
|
-
children: ReactNode
|
|
96
|
-
fallback?: ReactNode
|
|
97
|
-
errorFallback?: ReactNode | ((error: Error) => ReactNode)
|
|
98
|
-
}
|
|
85
|
+
children: ReactNode
|
|
86
|
+
fallback?: ReactNode
|
|
87
|
+
errorFallback?: ReactNode | ((error: Error) => ReactNode)
|
|
88
|
+
}
|
|
99
89
|
```
|
|
100
90
|
|
|
101
|
-
Top-level wrapper. It starts the handshake and renders `children` after init. It measures `#root`
|
|
91
|
+
Top-level wrapper. It starts the handshake and renders `children` after init. It measures `#root`
|
|
92
|
+
before it shows the iframe and when content height changes. `fallback` replaces the loading view.
|
|
93
|
+
Its default is `null`. `errorFallback` replaces the error message if init fails.
|
|
102
94
|
|
|
103
95
|
### `useCustomBlockInit()`
|
|
104
96
|
|
|
105
97
|
```ts
|
|
106
|
-
function useCustomBlockInit(): UseCustomBlockInitResult
|
|
98
|
+
function useCustomBlockInit(): UseCustomBlockInitResult
|
|
107
99
|
|
|
108
100
|
type UseCustomBlockInitResult =
|
|
109
101
|
| { isLoaded: false; error: undefined }
|
|
110
102
|
| { isLoaded: false; error: CustomBlockInitializationError }
|
|
111
|
-
| { isLoaded: true; error: undefined; initial: CustomBlockInitPayload }
|
|
103
|
+
| { isLoaded: true; error: undefined; initial: CustomBlockInitPayload }
|
|
112
104
|
```
|
|
113
105
|
|
|
114
|
-
React wrapper around `initCustomBlock` for templates that prefer not to use top-level `await`.
|
|
106
|
+
React wrapper around `initCustomBlock` for templates that prefer not to use top-level `await`.
|
|
107
|
+
Multiple components calling it share the same handshake.
|
|
115
108
|
|
|
116
109
|
```tsx
|
|
117
110
|
function Root() {
|
|
118
|
-
const init = useCustomBlockInit()
|
|
119
|
-
if (init.error) return <p role="alert">Init failed: {init.error.message}</p
|
|
120
|
-
if (!init.isLoaded) return null
|
|
121
|
-
return <App
|
|
111
|
+
const init = useCustomBlockInit()
|
|
112
|
+
if (init.error) return <p role="alert">Init failed: {init.error.message}</p>
|
|
113
|
+
if (!init.isLoaded) return null
|
|
114
|
+
return <App />
|
|
122
115
|
}
|
|
123
116
|
```
|
|
124
117
|
|
|
125
118
|
### `initCustomBlock()`
|
|
126
119
|
|
|
127
120
|
```ts
|
|
128
|
-
function initCustomBlock(): Promise<CustomBlockInitPayload
|
|
121
|
+
function initCustomBlock(): Promise<CustomBlockInitPayload>
|
|
129
122
|
```
|
|
130
123
|
|
|
131
|
-
The lower-level promise API. `<NotionCustomBlock>` and `useCustomBlockInit` both call it for you.
|
|
124
|
+
The lower-level promise API. `<NotionCustomBlock>` and `useCustomBlockInit` both call it for you.
|
|
125
|
+
Reach for it directly only when you want to `await` init at module scope (e.g. before
|
|
126
|
+
`ReactDOM.createRoot`). Expected failures reject with `CustomBlockInitializationError`.
|
|
132
127
|
|
|
133
128
|
### `CustomBlockInitializationError`
|
|
134
129
|
|
|
@@ -138,15 +133,19 @@ The `CustomBlockInitializationErrorCode` tells you where initialization failed:
|
|
|
138
133
|
- The SDK could not apply the host's state.
|
|
139
134
|
- The SDK failed before the handshake completed, such as `not_in_iframe`.
|
|
140
135
|
|
|
141
|
-
All code types are open string unions, so keep a default branch. Use `isRetryable` to decide whether
|
|
136
|
+
All code types are open string unions, so keep a default branch. Use `isRetryable` to decide whether
|
|
137
|
+
offering a retry makes sense, and treat `message` as display text rather than a stable identifier.
|
|
138
|
+
See [Error handling](./errors.md) for the shared error contract.
|
|
142
139
|
|
|
143
140
|
### `customBlock`
|
|
144
141
|
|
|
145
|
-
The SDK provides getters, subscriptions, and React hooks for block and app context. Use
|
|
142
|
+
The SDK provides getters, subscriptions, and React hooks for block and app context. Use
|
|
143
|
+
`customBlock` for getters and subscriptions.
|
|
146
144
|
|
|
147
145
|
#### Getter
|
|
148
146
|
|
|
149
|
-
Getters return the current value, such as the theme, current user, or containing page. They throw if
|
|
147
|
+
Getters return the current value, such as the theme, current user, or containing page. They throw if
|
|
148
|
+
called before initialization completes.
|
|
150
149
|
|
|
151
150
|
```ts
|
|
152
151
|
import { customBlock, initCustomBlock } from "@notionhq/custom-blocks"
|
|
@@ -158,7 +157,9 @@ console.log(theme)
|
|
|
158
157
|
|
|
159
158
|
#### Subscription
|
|
160
159
|
|
|
161
|
-
Subscriptions deliver the initial value and later changes. You can register them before
|
|
160
|
+
Subscriptions deliver the initial value and later changes. You can register them before
|
|
161
|
+
initialization. They wait for initialization before delivering the initial value, or deliver
|
|
162
|
+
immediately if initialization is complete.
|
|
162
163
|
|
|
163
164
|
```ts
|
|
164
165
|
import { customBlock, initCustomBlock } from "@notionhq/custom-blocks"
|
|
@@ -174,7 +175,9 @@ await initCustomBlock()
|
|
|
174
175
|
|
|
175
176
|
#### React hook
|
|
176
177
|
|
|
177
|
-
In React, use context hooks inside `NotionCustomBlock`. The wrapper waits for initialization, and
|
|
178
|
+
In React, use context hooks inside `NotionCustomBlock`. The wrapper waits for initialization, and
|
|
179
|
+
the hooks keep components current as values change. React handles subscription cleanup when the
|
|
180
|
+
component unmounts.
|
|
178
181
|
|
|
179
182
|
```tsx
|
|
180
183
|
import { NotionCustomBlock, useTheme } from "@notionhq/custom-blocks/react"
|
|
@@ -193,18 +196,25 @@ function ThemeLabel() {
|
|
|
193
196
|
}
|
|
194
197
|
```
|
|
195
198
|
|
|
196
|
-
See [Block location and appearance](./block-location.md#reading-values-and-following-changes) for
|
|
199
|
+
See [Block location and appearance](./block-location.md#reading-values-and-following-changes) for
|
|
200
|
+
the available getters, subscriptions, and React hooks.
|
|
197
201
|
|
|
198
202
|
### `NotInIframeError`
|
|
199
203
|
|
|
200
|
-
Thrown when `initCustomBlock` is called in a top-level tab (no parent frame). It extends
|
|
204
|
+
Thrown when `initCustomBlock` is called in a top-level tab (no parent frame). It extends
|
|
205
|
+
`CustomBlockInitializationError`, has `code: "not_in_iframe"` and `isRetryable: false`, and can be
|
|
206
|
+
detected specifically with `instanceof NotInIframeError`. `<NotionCustomBlock>` catches it and falls
|
|
207
|
+
back to a standalone preview with a warning banner.
|
|
201
208
|
|
|
202
209
|
## Debug console
|
|
203
210
|
|
|
204
|
-
Press `\` while focused in a custom block to toggle a debug overlay that replaces the block's
|
|
211
|
+
Press `\` while focused in a custom block to toggle a debug overlay that replaces the block's
|
|
212
|
+
children with a `<pre>` log of every `postMessage` sent and received over the bridge. Each line is
|
|
213
|
+
formatted as:
|
|
205
214
|
|
|
206
215
|
```
|
|
207
216
|
[ISO timestamp] sent/received: {"type":"connect", …}
|
|
208
217
|
```
|
|
209
218
|
|
|
210
|
-
The log is intentionally plain — no filtering or decoration — so it can be copied and pasted
|
|
219
|
+
The log is intentionally plain — no filtering or decoration — so it can be copied and pasted
|
|
220
|
+
directly to a local coding agent for debugging.
|