Skip to content

Commit

Permalink
feat: add selectedItem convenience param to usePuck
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvxd committed Mar 25, 2024
1 parent 8d36812 commit c1224d0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
18 changes: 14 additions & 4 deletions apps/docs/pages/docs/api-reference/functions/use-puck.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ export function Editor() {

## Returns

| Param | Example | Type |
| ----------------------- | ------------------------------ | ----------------------------------------- |
| [`appState`](#appstate) | `{ data: {}, ui: {} }` | [AppState](/docs/api-reference/app-state) |
| [`dispatch`](#dispatch) | `(action: PuckAction) => void` | Function |
| Param | Example | Type |
| ------------------------------- | ------------------------------------------------ | --------------------------------------------------- |
| [`appState`](#appstate) | `{ data: {}, ui: {} }` | [AppState](/docs/api-reference/app-state) |
| [`dispatch`](#dispatch) | `(action: PuckAction) => void` | Function |
| [`selectedItem`](#selecteditem) | `{ type: "Heading", props: {id: "my-heading"} }` | [ComponentData](/docs/api-reference/data#content-1) |

### `appState`

Expand All @@ -54,3 +55,12 @@ dispatch({
},
});
```

### `selectedItem`

The currently selected item, as defined by `appState.ui.itemSelector`.

```tsx
console.log(selectedItem);
// { type: "Heading", props: {id: "my-heading"} }
```
16 changes: 14 additions & 2 deletions packages/core/lib/use-puck.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import { useAppContext } from "../components/Puck/context";

export const usePuck = () => {
const { state: appState, config, history, dispatch } = useAppContext();
const {
state: appState,
config,
history,
dispatch,
selectedItem,
} = useAppContext();

return { appState, config, dispatch, history };
return {
appState,
config,
dispatch,
history,
selectedItem: selectedItem || null,
};
};

0 comments on commit c1224d0

Please sign in to comment.