You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
161
161
162
-
### Step 1: Install React Router
162
+
### Approach 1: File-System Based Routing (Recommended)
163
163
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.
165
165
166
-
```bash
167
-
cd .objectui-tmp
168
-
npm install react-router-dom
169
-
```
166
+
#### Directory Structure
170
167
171
-
### Step 2: Create Multiple Schema Files
168
+
Create a `pages/` directory with your schema files:
172
169
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
174
184
175
-
**home.schema.json:**
185
+
**pages/index.schema.json:**
176
186
```json
177
187
{
178
188
"type": "div",
@@ -188,7 +198,7 @@ Create separate schema files for each route:
188
198
}
189
199
```
190
200
191
-
**about.schema.json:**
201
+
**pages/about.schema.json:**
192
202
```json
193
203
{
194
204
"type": "div",
@@ -204,7 +214,84 @@ Create separate schema files for each route:
204
214
}
205
215
```
206
216
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
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
208
295
209
296
Update the generated `src/App.tsx` in `.objectui-tmp/src/App.tsx`:
210
297
@@ -270,7 +357,68 @@ You can also define navigation in your main schema using button click handlers:
270
357
}
271
358
```
272
359
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.
0 commit comments