@basenative/builder

中文文档
3.0.0发布于 2026/10/31Apache-2.0TypeScriptES Module构建来源已验证

Signal-based drag-and-drop component builder for BaseNative — composes layouts, generates clean BaseNative code

原始使用文档
文档目录10 节
  1. Install
  2. Quick start (programmatic)
  3. Quick start (in the browser)
  4. Signal bindings
  5. Public API
  6. Default palette
  7. Persistence
  8. Keyboard shortcuts (canvas)
  9. Tests
  10. License

@basenative/builder

Signal-based drag-and-drop component builder for BaseNative. Compose layouts visually, edit props through a reactive inspector, and generate clean BaseNative template code as output.

  • Zero dependencies beyond @basenative/runtime.
  • Dogfoods BaseNative signals — every piece of builder state is a signal().
  • Native HTML5 drag-and-drop — no shadow DOM, no custom DnD layer.
  • Semantic HTML with full ARIA roles (role="tree", role="application", role="toolbar").
  • display: contents on every host element — wrappers do not affect layout.
  • CSP-safe code generation — output is plain HTML/templates; no eval.

Install

pnpm add @basenative/builder

Quick start (programmatic)

import { createBuilderState, defaultPalette, generateBaseNative } from '@basenative/builder';

const palette = defaultPalette();
const state = createBuilderState();

const hero = state.addNode(null, { type: 'section', props: { class: 'hero' } });
state.addNode(hero.id, { type: 'heading', props: { level: 'h1', text: 'Welcome' } });
state.addNode(hero.id, { type: 'text', props: { text: 'A quiet runtime.' } });

console.log(generateBaseNative(state, { palette }));

Output:

<section class="hero" role="region">
  <h1>Welcome</h1>
  <p>A quiet runtime.</p>
</section>

Quick start (in the browser)

<bn-builder></bn-builder>

<script type="module">
  import '@basenative/builder';

  const builder = document.querySelector('bn-builder');
  builder.addEventListener('bn-builder-export', (e) => {
    console.log(e.detail.code);
  });
</script>

The <bn-builder> element mounts four panes:

ElementRole
<bn-builder-palette>Drag source for components
<bn-builder-canvas>Drop target — live preview of the tree
<bn-builder-tree>Hierarchical outline of nodes
<bn-builder-inspector>Reactive form for editing props and signal bindings

Signal bindings

Each node has a bindings map that wires a prop name to a signal in user scope. The codegen emits this as a BaseNative directive:

const node = state.addNode(null, { type: 'input' });
state.setBinding(node.id, 'value', { ref: 'username' });

generateBaseNative(state, { palette, signals: { username: '' } });

Output:

<script type="module">
  import { signal } from '@basenative/runtime';

  const username = signal("");
</script>
<input type="text" @bind="username" />

For text content:

state.setBinding(id, 'text', { ref: 'count' });
// → <span>{{ count() }}</span>

Public API

createBuilderState(options?): BuilderState
createPalette(): ComponentPalette
defaultPalette(): ComponentPalette
generateBaseNative(state, options?): string
renderTreeView(state): string
renderInspector(state, palette): string

BuilderState exposes signals (tree, selection, hover, canUndo, canRedo) and actions (addNode, removeNode, updateProps, setBinding, moveNode, duplicateNode, select, undo, redo, clear, toJSON, fromJSON, subscribe).

The signals are first-class — wire them into your own effect() to react to user edits:

import { effect } from '@basenative/runtime';

effect(() => console.log('Tree size:', state.tree().length));

Default palette

TypeTagContainerCategory
section<section role="region">yeslayout
stack<div>yeslayout
grid<div>yeslayout
heading<h1>–<h6>notext
text<p>notext
button<button>noinputs
input<input>noinputs
textarea<textarea>noinputs
checkbox<input type="checkbox">noinputs
label<label>yesinputs
form<form>yesinputs
link<a>nonavigation
image<img>nomedia
signal-text<span>{{ … }}</span>noreactive

Register your own:

palette.register({
  type: 'kbd',
  tag: 'kbd',
  category: 'text',
  props: [{ name: 'text', kind: 'string', label: 'Key' }],
  defaults: { text: '⌘K' },
});

Persistence

const json = state.toJSON();
// store somewhere

state.fromJSON(json); // wipes history, hydrates tree

Keyboard shortcuts (canvas)

KeysAction
Delete / BackspaceRemove selected node
Cmd/Ctrl + ZUndo
Cmd/Ctrl + Shift + Z / Cmd/Ctrl + YRedo

Tests

cd packages/builder && node --test

Tests are written with node:test — no Jest, no Vitest, no browser harness required for the pure logic. Web component tests require a DOM and are exercised in downstream apps.

License

Apache-2.0

版本与发布记录

@basenative/builder 共发布 11 个版本,首次发布于 2026/10/18;过去 12 个月发布了 11 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.0.0latest2026/10/312 天前
2.4.02026/10/268 天前
2.3.22026/10/2212 天前
2.3.12026/10/2212 天前
2.3.02026/10/2212 天前
2.2.02026/10/2113 天前

关于 @basenative/builder 的常见问题

@basenative/builder 是什么?

用于BaseNative的基于信号的拖放组件构建器-组合布局,生成干净的BaseNative代码

如何安装 @basenative/builder?

在项目目录运行 npm install @basenative/builder,或使用 pnpm add / yarn add / bun add。

@basenative/builder 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@basenative/builder 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@basenative/builder 可以商用吗?

@basenative/builder 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@basenative/builder 还在维护吗?

最新版本 3.0.0 发布于 2026/10/31(2 天前);至今共发布 11 个版本,其中过去 12 个月发布了 11 个;首次发布于 2026/10/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。