Skip to content

Commit cd26724

Browse files
Copilothotlong
andcommitted
Add Next.js-style file-system routing documentation to CLI guide
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 2d52763 commit cd26724

1 file changed

Lines changed: 161 additions & 13 deletions

File tree

docs/CLI_GUIDE.md

Lines changed: 161 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,22 +157,32 @@ objectui serve app.schema.json --host 0.0.0.0 --port 3001
157157

158158
## Adding Routing to Your Application
159159

160-
By default, the CLI generates a single-page application. To add routing capabilities, you'll need to manually set up React Router in your generated application.
160+
The CLI supports two approaches for routing: **file-system based routing** (recommended, Next.js-style) and manual React Router setup.
161161

162-
### Step 1: Install React Router
162+
### Approach 1: File-System Based Routing (Recommended)
163163

164-
Navigate to your `.objectui-tmp` directory after running `objectui serve`:
164+
Organize your schema files in a `pages/` directory structure, and the CLI will automatically generate routes based on the folder hierarchy.
165165

166-
```bash
167-
cd .objectui-tmp
168-
npm install react-router-dom
169-
```
166+
#### Directory Structure
170167

171-
### Step 2: Create Multiple Schema Files
168+
Create a `pages/` directory with your schema files:
172169

173-
Create separate schema files for each route:
170+
```
171+
my-app/
172+
├── pages/
173+
│ ├── index.schema.json → /
174+
│ ├── about.schema.json → /about
175+
│ ├── blog/
176+
│ │ ├── index.schema.json → /blog
177+
│ │ └── [id].schema.json → /blog/:id
178+
│ └── users/
179+
│ └── [userId].schema.json → /users/:userId
180+
└── app.schema.json (optional layout)
181+
```
182+
183+
#### Example Page Schemas
174184

175-
**home.schema.json:**
185+
**pages/index.schema.json:**
176186
```json
177187
{
178188
"type": "div",
@@ -188,7 +198,7 @@ Create separate schema files for each route:
188198
}
189199
```
190200

191-
**about.schema.json:**
201+
**pages/about.schema.json:**
192202
```json
193203
{
194204
"type": "div",
@@ -204,7 +214,84 @@ Create separate schema files for each route:
204214
}
205215
```
206216

207-
### Step 3: Modify App.tsx
217+
**pages/blog/[id].schema.json** (Dynamic route):
218+
```json
219+
{
220+
"type": "div",
221+
"className": "p-8",
222+
"body": {
223+
"type": "card",
224+
"title": "Blog Post",
225+
"body": {
226+
"type": "text",
227+
"content": "Blog post ID: ${params.id}"
228+
}
229+
}
230+
}
231+
```
232+
233+
#### Running with File-System Routing
234+
235+
When you have a `pages/` directory, the CLI automatically detects it and sets up routing:
236+
237+
```bash
238+
objectui serve
239+
```
240+
241+
The serve command will:
242+
1. Detect the `pages/` directory
243+
2. Automatically install `react-router-dom`
244+
3. Generate route configuration based on file structure
245+
4. Set up the application with all routes
246+
247+
#### Route Mapping Rules
248+
249+
- `pages/index.schema.json``/`
250+
- `pages/about.schema.json``/about`
251+
- `pages/blog/index.schema.json``/blog`
252+
- `pages/blog/[id].schema.json``/blog/:id` (dynamic parameter)
253+
- `pages/users/[userId]/posts/[postId].schema.json``/users/:userId/posts/:postId`
254+
255+
#### Navigation Between Pages
256+
257+
Use the button component with `href` to navigate:
258+
259+
```json
260+
{
261+
"type": "button",
262+
"label": "Go to About",
263+
"href": "/about"
264+
}
265+
```
266+
267+
Or use link components:
268+
269+
```json
270+
{
271+
"type": "link",
272+
"href": "/blog/123",
273+
"children": "Read Blog Post"
274+
}
275+
```
276+
277+
### Approach 2: Manual React Router Setup
278+
279+
For more control, you can manually set up React Router in the generated application.
280+
281+
#### Step 1: Install React Router
282+
283+
Navigate to your `.objectui-tmp` directory after running `objectui serve`:
284+
285+
```bash
286+
cd .objectui-tmp
287+
npm install react-router-dom
288+
```
289+
290+
#### Step 2: Create Multiple Schema Files
291+
292+
Create separate schema files for each route.
293+
294+
#### Step 3: Modify App.tsx
208295

209296
Update the generated `src/App.tsx` in `.objectui-tmp/src/App.tsx`:
210297

@@ -270,7 +357,68 @@ You can also define navigation in your main schema using button click handlers:
270357
}
271358
```
272359

273-
**Note:** Full schema-based routing support is planned for a future release. Currently, routing requires manual React Router setup in the generated code.
360+
## Layouts and Nested Routes
361+
362+
### Root Layout
363+
364+
Create an `app.schema.json` file to define a layout that wraps all pages:
365+
366+
```json
367+
{
368+
"type": "div",
369+
"className": "min-h-screen",
370+
"body": [
371+
{
372+
"type": "div",
373+
"className": "border-b bg-background",
374+
"body": {
375+
"type": "div",
376+
"className": "container mx-auto px-6 py-4",
377+
"body": [
378+
{
379+
"type": "link",
380+
"href": "/",
381+
"className": "mr-4",
382+
"children": "Home"
383+
},
384+
{
385+
"type": "link",
386+
"href": "/about",
387+
"className": "mr-4",
388+
"children": "About"
389+
},
390+
{
391+
"type": "link",
392+
"href": "/blog",
393+
"children": "Blog"
394+
}
395+
]
396+
}
397+
},
398+
{
399+
"type": "div",
400+
"className": "container mx-auto",
401+
"body": "{{outlet}}"
402+
}
403+
]
404+
}
405+
```
406+
407+
The `{{outlet}}` placeholder is where page content will be rendered.
408+
409+
### Nested Layouts
410+
411+
Create `_layout.schema.json` files in subdirectories for nested layouts:
412+
413+
```
414+
pages/
415+
├── blog/
416+
│ ├── _layout.schema.json → Layout for all /blog routes
417+
│ ├── index.schema.json → /blog
418+
│ └── [id].schema.json → /blog/:id
419+
```
420+
421+
**Note:** File-system based routing with automatic route generation is planned for a future release. The documentation above describes the intended behavior.
274422

275423
## FAQ
276424

0 commit comments

Comments
 (0)