Skip to content

Commit 9a1ada5

Browse files
committed
Support Next client usage
1 parent e14fb3a commit 9a1ada5

9 files changed

Lines changed: 89 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
- Added React `CodexPetProvider`, `useCodexPet`, and `useCodexPetRandomActions`.
77
- Added `hide`, `show`, and `remove` controller commands.
88
- Added idle-only weighted random action runners with configurable average intervals.
9+
- Marked React entry points as client modules for Next.js App Router usage.
10+
- Fixed drag state flicker when unchanged controlled state props are reapplied.
911
- Updated the demo to start slightly smaller and merge bundled examples with local pets.
1012
- Removed the old Vertical fixture from the repository.
1113

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ Framework-neutral DOM usage:
2727
npm install codex-pet-web
2828
```
2929

30+
## Next.js
31+
32+
Use `codex-pet-web-react` from a client component. The React package ships
33+
`"use client"` markers, while `codex-pet-web` stays framework-neutral for shared
34+
types, constants, and browser runtime utilities.
35+
3036
## Quick Start: React
3137

3238
```tsx

packages/core/src/registry.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,10 @@ class CodexPetControllerImpl implements CodexPetController {
153153

154154
setConfig(config: Partial<CodexPetConfig>): void {
155155
this.assertNotRemoved();
156+
const previousState = this.config.state ?? DEFAULT_STATE;
156157
this.config = { ...this.config, ...config };
157158

158-
if (config.state) {
159+
if (config.state && config.state !== previousState) {
159160
this.baseState = config.state;
160161
this.state = config.state;
161162
this.animator?.setBaseState(config.state, { interrupt: true });

packages/core/test/registry.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,46 @@ describe("Codex pet registry", () => {
144144
position: { x: 100, y: 120 }
145145
});
146146
});
147+
148+
it("does not reset temporary drag state when unchanged config is applied", () => {
149+
const registry = createCodexPetRegistry({
150+
pets: {
151+
assistant: {
152+
spritesheetUrl: "/pets/sapling/spritesheet.webp",
153+
state: "idle",
154+
floating: true,
155+
draggable: true
156+
}
157+
}
158+
});
159+
const assistant = registry.get("assistant");
160+
const element = bind(assistant);
161+
162+
element.dispatchEvent(
163+
new MouseEvent("pointerdown", {
164+
bubbles: true,
165+
button: 0,
166+
clientX: 20,
167+
clientY: 30
168+
})
169+
);
170+
window.dispatchEvent(
171+
new MouseEvent("pointermove", {
172+
bubbles: true,
173+
clientX: 40,
174+
clientY: 30
175+
})
176+
);
177+
178+
expect(assistant.getSnapshot().state).toBe("running-right");
179+
180+
assistant.setConfig({
181+
spritesheetUrl: "/pets/sapling/spritesheet.webp",
182+
state: "idle",
183+
floating: true,
184+
draggable: true
185+
});
186+
187+
expect(assistant.getSnapshot().state).toBe("running-right");
188+
});
147189
});

packages/react/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,35 @@ npm install codex-pet-web-react codex-pet-web
1010

1111
`react` is a peer dependency.
1212

13+
## Next.js
14+
15+
`codex-pet-web-react` is client-only and ships `"use client"` markers. In a
16+
Next.js App Router app, import it from a client component and render that client
17+
component from your layout or page:
18+
19+
```tsx
20+
"use client";
21+
22+
import { CodexPet, CodexPetProvider } from "codex-pet-web-react";
23+
24+
export function PetLayer() {
25+
return (
26+
<CodexPetProvider
27+
pets={{
28+
assistant: {
29+
spritesheetUrl: "/codex-pets/sapling/spritesheet.webp",
30+
scale: 0.45,
31+
floating: { x: 24, y: 24 },
32+
draggable: true
33+
}
34+
}}
35+
>
36+
<CodexPet id="assistant" />
37+
</CodexPetProvider>
38+
);
39+
}
40+
```
41+
1342
## Basic Usage
1443

1544
```tsx

packages/react/src/CodexPet.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"use client";
2+
13
import {
24
createCodexPetRegistry,
35
type CodexPetAnimationEvent,

packages/react/src/CodexPetProvider.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"use client";
2+
13
import {
24
createCodexPetRegistry,
35
type CodexPetConfig,

packages/react/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"use client";
2+
13
export { CodexPet } from "./CodexPet.js";
24
export type { CodexPetHandle, CodexPetProps } from "./CodexPet.js";
35
export {

packages/react/src/useCodexPetRandomActions.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"use client";
2+
13
import {
24
createCodexPetRandomActionRunner,
35
type CodexPetId,

0 commit comments

Comments
 (0)