### Install solid-nest with npm, yarn, or pnpm
Source: https://github.com/rafferty97/solid-nest/blob/main/README.md
This code snippet shows the commands to install the solid-nest library using different package managers: npm, yarn, and pnpm. Choose the command that corresponds to your project's package manager.
```bash
npm i solid-nest
# or
yarn add solid-nest
# or
pnpm add solid-nest
```
--------------------------------
### Configure BlockTree with getOptions Example
Source: https://github.com/rafferty97/solid-nest/blob/main/README.md
Demonstrates how to use the getOptions prop within a BlockTree component to dynamically set configuration for each block based on its type. This example shows conditional spacing, tag assignment, and accepted child tags.
```tsx
block.id}
getChildren={block => block.children}
getOptions={block => ({
spacing: block.type === 'container' ? 20 : 12,
tag: block.type,
accepts: block.type === 'container' ? ['item'] : []
})}>
{/* ... */}
```
--------------------------------
### Solid-Nest BlockTree State Management Example
Source: https://github.com/rafferty97/solid-nest/blob/main/README.md
Demonstrates basic state management for the BlockTree component using SolidJS signals. It shows how to handle root block state, selection state, and reorder events.
```tsx
import { createSignal } from 'solid-js'
import { BlockTree } from 'solid-nest'
type MyBlock = {
id: string
text: string
children?: MyBlock[]
}
function App() {
const [root, setRoot] = createSignal({
id: 'root',
text: 'Root',
children: []
})
const [selection, setSelection] = createSignal<{ blocks?: string[] }>({})
const handleReorder = (event: ReorderEvent) => {
// Update your state to reflect the reordering
// Implementation depends on your state structure
}
return (
block.id}
getChildren={block => block.children}
selection={selection()}
onSelectionChange={event => {
if (event.kind === 'blocks') {
setSelection({ blocks: event.blocks })
} else if (event.kind === 'deselect') {
setSelection({})
}
}}
onReorder={handleReorder}
>
{props => (
{props.block.text}
{props.children}
)}
)
}
```
--------------------------------
### Basic SolidJS App with BlockTree Component
Source: https://github.com/rafferty97/solid-nest/blob/main/README.md
This example demonstrates how to set up a basic SolidJS application using the `BlockTree` component from the `solid-nest` library. It defines a `MyBlock` type, initializes a root block structure, and renders the `BlockTree` with custom block rendering logic.
```tsx
import { BlockTree, createBlockTree } from 'solid-nest'
type MyBlock = {
id: string
text: string
children?: MyBlock[]
}
function App() {
// Define your block structure
const root: MyBlock = {
id: 'root',
text: 'Root',
children: [
{ id: 'a', text: 'First block' },
{ id: 'b', text: 'Second block' },
{ id: 'c', text: 'Third block' },
],
}
return (
block.id}
getChildren={block => block.children}
>
{/* Defines how each block in the tree should be rendered */}
{props => (
{/* Add data-drag-handle to elements that should initiate drag */}
{props.block.text}
{props.children}
)}
)
}
```
--------------------------------
### Customize BlockTree Dropzone Component
Source: https://github.com/rafferty97/solid-nest/blob/main/README.md
This example illustrates how to provide a custom `Dropzone` component to the `BlockTree`. This allows for visual customization of the area where dragged blocks will be placed upon release. The component can be styled to fill the available space.
```tsx
const Dropzone = () => (
Drop here
)
{/* ... */}
```
--------------------------------
### CopyEvent and Clipboard Data Handling
Source: https://github.com/rafferty97/solid-nest/blob/main/README.md
Details the CopyEvent structure, including the blocks being copied and the DataTransfer object. Provides an example of how to set clipboard data for JSON and plain text formats.
```tsx
type CopyEvent = {
blocks: T[] // Blocks being copied
data: DataTransfer // Clipboard data transfer object
}
onCopy={(event) => {
const json = JSON.stringify(event.blocks)
event.data.setData('application/json', json)
event.data.setData('text/plain', `Copied ${event.blocks.length} blocks`)
}}
```
--------------------------------
### Configure Block Nesting with Tags in BlockTree
Source: https://github.com/rafferty97/solid-nest/blob/main/README.md
This example demonstrates how to define and apply tags to control which blocks can be nested within others in a BlockTree. The `getOptions` function is used to specify the `tag` for a block and the types of blocks it `accepts` as children.
```tsx
type MyBlock = {
id: string
type: 'container' | 'item'
text: string
children?: MyBlock[]
}
const root: MyBlock = {
id: 'root',
type: 'container',
text: 'Root',
children: [
{
id: 'container',
type: 'container',
text: 'Container',
children: [],
},
{
id: 'item1',
type: 'item',
text: 'Item',
},
],
}
block.id}
getChildren={block => block.children}
getOptions={block => ({
tag: block.type,
accepts: block.type === 'container' ? ['item'] : []
})}
>
{/* ... */}
```
--------------------------------
### PasteEvent Structure and Data Parsing
Source: https://github.com/rafferty97/solid-nest/blob/main/README.md
Describes the PasteEvent, which includes the target placement for pasting and the DataTransfer object. Includes an example of parsing JSON data from the clipboard and preparing to insert blocks.
```tsx
type PasteEvent = {
place: Place // Where the data should be pasted
data: DataTransfer // Clipboard data transfer object
}
onPaste={(event) => {
const json = event.data.getData('application/json')
if (json) {
const blocks = JSON.parse(json)
// Insert blocks at `event.place`
}
}}
```
--------------------------------
### Manage BlockTree State with createBlockTree (SolidJS)
Source: https://context7.com/rafferty97/solid-nest/llms.txt
Utilizes the `createBlockTree` helper function to manage the state for the BlockTree component. This example demonstrates adding new blocks, updating block data via an input field, and integrating with the `BlockTree` component. It simplifies state management by providing event handlers directly.
```tsx
import { createBlockTree, BlockTree } from 'solid-nest'
import { createUniqueId } from 'solid-js'
type MyBlock = {
key: string
data: string
children?: MyBlock[]
}
function App() {
const initialData: MyBlock = {
key: 'root',
data: '',
children: [
{ key: '1', data: 'Drag me' },
{ key: '2', data: 'Or drag me' },
{ key: '3', data: 'Reorder us!' },
],
}
const props = createBlockTree(initialData)
const addBlock = () => {
props.onInsert({
place: { parent: props.root.key, before: null },
blocks: [{ key: createUniqueId(), data: 'New block' }],
})
}
const updateBlockData = (key: string, newData: string) => {
props.updateBlock(key, { data: newData })
}
return (
)
}
```
--------------------------------
### Implement Draggable Block with Event Handling
Source: https://github.com/rafferty97/solid-nest/blob/main/README.md
Shows how to make a block element draggable by adding the 'data-drag-handle' attribute. It also includes an example of preventing event propagation for focusable elements like inputs within a draggable block.
```tsx
props => (
{props.block.text}
ev.stopPropagation()} />
)
```
--------------------------------
### Customizing Solid-Nest Components: Dropzone, Placeholder, DragContainer (TypeScript/JSX)
Source: https://context7.com/rafferty97/solid-nest/llms.txt
Replace default Solid-Nest UI components for dropzones, placeholders, and drag containers with custom implementations. This example shows how to define and use custom React components for these elements to tailor the user interface and experience. Dependencies include 'solid-nest' and 'solid-js'.
```tsx
import { BlockTree, createBlockTree, DragContainerProps } from 'solid-nest'
import { Show } from 'solid-js'
type MyBlock = {
key: string
text: string
children?: MyBlock[]
}
const CustomDropzone = () => (
)}
)
}
```
--------------------------------
### Custom Block Type Definition for solid-nest
Source: https://github.com/rafferty97/solid-nest/blob/main/README.md
This example shows how to define a custom TypeScript type for your blocks when using the `solid-nest` library. The `BlockTree` component is flexible and does not enforce a specific block structure, allowing you to define properties like `id`, `text`, and `children` as needed.
```typescript
type MyBlock = {
id: string
text: string
children?: MyBlock[]
}
block.id}
getChildren={block => block.children}
>
{/* ... */}
```
--------------------------------
### BlockTree Component Configuration and Props
Source: https://github.com/rafferty97/solid-nest/blob/main/README.md
This snippet illustrates the various props available for the `BlockTree` component in `solid-nest`. It covers essential props for defining the tree structure, handling selections, and managing events like insertion, reordering, and removal.
```tsx
block.id}
getChildren={block => block.children}
getOptions={block => ({ spacing: 16, tag: 'item' })}
// The currently selected blocks
selection={{ blocks: ['key1', 'key2'] }}
// Various event handlers; because this is a controlled component,
// the state of the tree won't update unless these are handled
onSelectionChange={event => {}}
onInsert={event => {}}
onReorder={event => {}}
onRemove={event => {}}
// ...various additional events and configuration props, documented below
>
{/* A function to render each block in the tree */}
{props => }
```
--------------------------------
### Selection Types
Source: https://github.com/rafferty97/solid-nest/blob/main/README.md
Explains the structure of the `Selection` object used for tracking selected blocks or insertion points.
```APIDOC
## Selection Types
### Description
This section describes the `Selection` type, which represents the current selection state within the `BlockTree`, including selected blocks or insertion points.
### Method
N/A (Type Definition)
### Endpoint
N/A
### Parameters
#### Path Parameters
N/A
#### Query Parameters
N/A
#### Request Body
N/A
### Request Example
N/A
### Response
#### Success Response (200)
N/A
#### Response Example
N/A
### Type Definition
```ts
type Selection = {
blocks?: K[]
place?: Place
}
```
### Notes
A `Selection` can represent either a set of blocks or a specific insertion place, but not both simultaneously.
```
--------------------------------
### BlockTree Props API
Source: https://github.com/rafferty97/solid-nest/blob/main/README.md
This section details the various props accepted by the BlockTree component for configuration and behavior.
```APIDOC
## BlockTree Props API
### Description
The `BlockTree` component accepts a range of props to customize its functionality, including data handling, event management, and visual appearance.
### Method
N/A (Component Props)
### Endpoint
N/A (Component)
### Parameters
#### Path Parameters
N/A
#### Query Parameters
N/A
#### Request Body
N/A
### Request Example
N/A
### Response
#### Success Response (200)
N/A
#### Response Example
N/A
### Props Table
| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `root` | `R` | *required* | The root block of the tree |
| `children` | `Component>` | *required* | Render function for blocks |
| `getKey` | `(block: T | R) => K` | *required* | Function to get a block's unique key |
| `getChildren` | `(block: T | R) => T[] | null | undefined` | | Function to get a block's children |
| `getOptions` | `(block: T | R) => BlockOptions | null | undefined` | | Function to get a block's options |
| `selection` | `Selection` | | Current selection |
| `onSelectionChange` | `(event: SelectionEvent) => void` | | Called when selection changes |
| `onInsert` | `EventHandler>` | | Called when blocks are inserted |
| `onReorder` | `EventHandler>` | | Called when blocks are reordered |
| `onRemove` | `EventHandler>` | | Called when blocks are removed |
| `onCopy` | `EventHandler>` | | Called when blocks are copied |
| `onCut` | `EventHandler>` | | Called when blocks are cut |
| `onPaste` | `EventHandler>` | | Called when blocks are pasted |
| `transitionDuration` | `number` | `200` | Animation duration (ms) |
| `dragThreshold` | `number` | `10` | Distance cursor must move (px) to start drag |
| `fixedHeightWhileDragging` | `boolean` | `false` | Fix container height during drag operations |
| `multiselect` | `boolean` | `true` | Enable multi-selection |
| `dropzone` | `Component<{}>` | | Custom dropzone component |
| `placeholder` | `Component<{ parent: K }>` | | Custom placeholder component |
| `dragContainer` | `Component>` | | Custom drag container component |
```
--------------------------------
### Block Options Type
Source: https://github.com/rafferty97/solid-nest/blob/main/README.md
Defines the structure for block-specific configuration options returned by `getOptions`.
```APIDOC
## Block Options Type
### Description
The `BlockOptions` type specifies the configuration settings that can be provided for individual blocks within the `BlockTree`.
### Method
N/A (Type Definition)
### Endpoint
N/A
### Parameters
#### Path Parameters
N/A
#### Query Parameters
N/A
#### Request Body
N/A
### Request Example
N/A
### Response
#### Success Response (200)
N/A
#### Response Example
N/A
### Type Definition
```ts
type BlockOptions = {
spacing?: number // Spacing between children (in pixels)
tag?: string // Tag for drag-and-drop constraints
accepts?: string[] // Array of tags this block accepts as children
}
```
```
--------------------------------
### Block Render Props
Source: https://github.com/rafferty97/solid-nest/blob/main/README.md
Details the props passed to the custom render function for each block.
```APIDOC
## Block Render Props
### Description
Describes the props provided to the custom render function for each block in the `BlockTree`, enabling detailed control over block appearance and behavior.
### Method
N/A (Render Function Props)
### Endpoint
N/A
### Parameters
#### Path Parameters
N/A
#### Query Parameters
N/A
#### Request Body
N/A
### Request Example
```tsx
props => (
{props.block.text}
ev.stopPropagation()} />
)
```
### Response
#### Success Response (200)
N/A
#### Response Example
N/A
### Props Table
| Prop | Type | Description |
|------|------|-------------|
| `key` | `K` | Block's unique key |
| `block` | `T` | The block data |
| `selected` | `boolean` | Whether block is currently selected |
| `dragging` | `boolean` | Whether block is being dragged |
| `children` | `JSX.Element` | Rendered child blocks |
### Notes
- The `children` prop should be rendered to display nested blocks.
- To allow elements within a block to receive focus (e.g., input fields), use `event.stopPropagation()` on the `onPointerDown` event handler to prevent the block itself from being selected.
```
--------------------------------
### Render BlockTree with Custom UI (SolidJS)
Source: https://context7.com/rafferty97/solid-nest/llms.txt
Demonstrates how to use the BlockTree component to render a hierarchical structure. It includes drag-and-drop handles, selection styling, and basic event handling for selection, reordering, and removal. Customizes the appearance of each block based on its selected and dragging state.
```tsx
import { BlockTree, createBlockTree } from 'solid-nest'
import { createSignal } from 'solid-js'
type MyBlock = {
key: string
text: string
children?: MyBlock[]
}
function App() {
const root: MyBlock = {
key: 'root',
text: 'Root',
children: [
{ key: 'a', text: 'First block' },
{ key: 'b', text: 'Second block', children: [
{ key: 'c', text: 'Nested block' }
]},
],
}
const [selection, setSelection] = createSignal<{ blocks?: string[] }>({})
return (
block.key}
getChildren={block => block.children}
selection={selection()}
onSelectionChange={event => {
if (event.kind === 'blocks') {
setSelection({ blocks: event.blocks })
} else if (event.kind === 'deselect') {
setSelection({})
}
}}
onReorder={event => {
console.log('Reorder blocks:', event.keys, 'to:', event.place)
}}
onRemove={event => {
console.log('Remove blocks:', event.keys)
}}
>
{props => (
{props.block.text}
{props.children}
)}
)
}
```
--------------------------------
### Customize BlockTree Placeholder Component
Source: https://github.com/rafferty97/solid-nest/blob/main/README.md
This code snippet shows how to replace the default `Placeholder` component in `BlockTree` with a custom implementation. The custom component receives the parent block's key and can be used to display custom messages or UI elements when a block has no children.
```tsx
const Placeholder = ({ parent }) => (
No items in {parent}
)
{/* ... */}
```
--------------------------------
### Handle Block Selection in Solid-Nest (TypeScript/React)
Source: https://context7.com/rafferty97/solid-nest/llms.txt
Demonstrates how to manage block selection modes (single, multi, range) using the BlockTree component in Solid-Nest. It utilizes the `selection` and `onSelectionChange` props to track and update selected blocks or insertion points. Dependencies include 'solid-nest'.
```tsx
import { BlockTree, Selection, SelectionEvent } from 'solid-nest'
type MyBlock = {
key: string
text: string
children?: MyBlock[]
}
function App() {
const [root, setRoot] = createSignal({
key: 'root',
text: '',
children: [
{ key: '1', text: 'Click me' },
{ key: '2', text: 'Cmd+Click to multi-select' },
{ key: '3', text: 'Shift+Click for range select' },
]
})
const [selection, setSelection] = createSignal>({})
const handleSelectionChange = (event: SelectionEvent) => {
if (event.kind === 'blocks') {
console.log('Selection mode:', event.mode) // 'set', 'toggle', or 'range'
console.log('Selected blocks:', event.blocks)
setSelection({ blocks: event.blocks })
} else if (event.kind === 'place') {
console.log('Selected insertion point:', event.place)
setSelection({ place: event.place })
} else if (event.kind === 'deselect') {
console.log('Deselected')
setSelection({})
}
}
return (
block.key}
getChildren={block => block.children}
selection={selection()}
onSelectionChange={handleSelectionChange}
multiselect={true}
>
{props => (