Skip to content

Commit 70c81ac

Browse files
committed
Add types and tweak styles
1 parent 0df5111 commit 70c81ac

File tree

13 files changed

+283
-39
lines changed

13 files changed

+283
-39
lines changed

src/css/custom.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ h1 {
660660
}
661661

662662
.markdown h1, .markdown h1:first-child, .index-page h1 {
663-
--ifm-h1-font-size: 2.5rem;
663+
--ifm-h1-font-size: 1.875rem;
664664
}
665665

666666
h2 {
@@ -781,7 +781,7 @@ a:hover {
781781
article,
782782
.index-page {
783783
margin: 0 auto;
784-
max-width: 66ch;
784+
max-width: 50rem;
785785
}
786786

787787
/* back to top button */
@@ -1077,7 +1077,7 @@ html [class*='toggleButton'] {
10771077

10781078
.theme-doc-sidebar-item-category-level-1 a,
10791079
.theme-doc-sidebar-item-link-level-1 a {
1080-
color: var(--st-text);
1080+
color: var(--ifm-color-emphasis-0);
10811081
}
10821082

10831083
.theme-doc-sidebar-item-category-level-1 ul li,

versioned_docs/version-5/actions.mdx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,29 @@ const countMachine = createMachine({
524524

525525
## TypeScript
526526

527-
_Coming soon_
527+
You can strongly type the `actions` of your machine in the `types.actions` property of the machine config.
528+
529+
```ts
530+
const machine = createMachine({
531+
types: {} as {
532+
// highlight-start
533+
actions:
534+
| {
535+
type: 'track';
536+
params: {
537+
response: string;
538+
};
539+
}
540+
| { type: 'increment'; params: { value: number } };
541+
// highlight-end
542+
},
543+
// ...
544+
entry: [
545+
{ type: 'track', params: { response: 'good' } },
546+
{ type: 'increment', params: { value: 1 } },
547+
],
548+
});
549+
```
528550

529551
## Cheatsheet
530552

versioned_docs/version-5/actors.mdx

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,39 @@ function Component(props: { actor?: AnyActorRef }) {
413413

414414
## TypeScript
415415

416-
_Coming soon_
416+
You can strongly type the `actors` of your machine in the `types.actors` property of the machine config.
417+
418+
```ts
419+
const fetcher = fromPromise(
420+
async ({ input }: { input: { userId: string } }) => {
421+
const user = await fetchUser(input.userId);
422+
423+
return user;
424+
},
425+
);
426+
427+
const machine = createMachine({
428+
types: {} as {
429+
// highlight-start
430+
actors: {
431+
src: 'fetchData'; // src name (inline behaviors ideally inferred)
432+
id: 'fetch1' | 'fetch2'; // possible ids (optional)
433+
logic: typeof fetcher;
434+
};
435+
// highlight-end
436+
},
437+
invoke: {
438+
src: 'fetchData', // strongly typed
439+
id: 'fetch2', // strongly typed
440+
onDone: {
441+
actions: ({ event }) => {
442+
event.output; // strongly typed as { result: string }
443+
},
444+
},
445+
input: { userId: '42' }, // strongly typed
446+
},
447+
});
448+
```
417449

418450
## Cheatsheet
419451

versioned_docs/version-5/context.mdx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,22 @@ feedbackActor.send({
151151

152152
## TypeScript
153153

154+
You can strongly type the `context` of your machine in the `types.context` property of the machine config.
155+
154156
```ts
155157
const machine = createMachine({
156-
schema: {} as {
158+
types: {} as {
159+
// highlight-start
157160
context: {
158161
feedback: string;
159162
rating: number;
160163
};
164+
// highlight-end
165+
},
166+
// Initial context
167+
context: {
168+
feedback: '',
169+
rating: 5,
161170
},
162171
entry: ({ context }) => {
163172
context.feedback; // string

versioned_docs/version-5/delayed-transitions.mdx

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ Delayed transitions have a default time interval of 500ms (0.5 seconds).
7272

7373
1. Select the event you want to replace with a delayed transition.
7474
1. Right-click the event to open the edit menu.
75-
2. From the **Event type** options, choose **After** to turn the event into a delayed transition.
75+
1. From the **Event type** options, choose **After** to turn the event into a delayed transition.
7676

7777
#### Using the transition Details panel
7878

7979
1. Open the transition <Info size={18} /> **Details** panel from the right tool menu.
8080
2. From the **Event type** dropdown menu, choose **After** to turn the event into a delayed transition.
8181

82-
#### Specify delay time
82+
#### Specify delay time
8383

8484
Your delay time interval will be displayed in a human-readable format on hover. For example, 15000ms will be displayed as 15 seconds.
8585

@@ -160,7 +160,36 @@ Delayed transition timers are canceled when the state is exited.
160160

161161
## TypeScript
162162

163-
_Coming soon_
163+
You can strongly type the `delays` of your machine in the `types.delays` property of the machine config.
164+
165+
```ts
166+
const machine = createMachine({
167+
types: {} as {
168+
// highlight-start
169+
delays: 'shortTimeout' | 'longTimeout' | 'eventually';
170+
// highlight-end
171+
// ...
172+
},
173+
// ...
174+
after: {
175+
// Autocompleted
176+
shortTimeout: {
177+
/* ... */
178+
},
179+
},
180+
on: {
181+
someEvent: {
182+
actions: raise(
183+
{ type: 'anotherEvent' },
184+
{
185+
// Autocompleted
186+
delay: 'eventually',
187+
},
188+
),
189+
},
190+
},
191+
});
192+
```
164193

165194
## Cheatsheet
166195

versioned_docs/version-5/eventless-transitions.mdx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ Eventless transitions are labeled “always” and often referred to as “alway
2626
}
2727
```
2828

29-
<SkipDownLink text="Jump to learning more about eventless transitions in XState" link="#eventless-transitions-and-guards"/>
29+
<SkipDownLink
30+
text="Jump to learning more about eventless transitions in XState"
31+
link="#eventless-transitions-and-guards"
32+
/>
3033

3134
## Using eventless transitions in Stately Studio
3235

@@ -77,7 +80,24 @@ Coming soon… examples
7780

7881
## TypeScript
7982

80-
_Coming soon_
83+
Eventless transitions can potentially be enabled by any event, so the `event` type is the union of all possible events.
84+
85+
```ts
86+
const machine = createMachine({
87+
types: {} as {
88+
events: { type: 'greet'; message: string } | { type: 'submit' };
89+
},
90+
// ...
91+
always: {
92+
actions: ({ event }) => {
93+
event.type; // 'greet' | 'submit'
94+
},
95+
guard: ({ event }) => {
96+
event.type; // 'greet' | 'submit'
97+
},
98+
},
99+
});
100+
```
81101

82102
## Cheatsheet
83103

versioned_docs/version-5/finite-states.mdx

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -167,25 +167,7 @@ console.log(feedbackActor..getSnapshot().hasTag('visible'));
167167
// logs true
168168
```
169169

170-
### Using tags in Stately Studio
171-
172-
You can add tags to states and events in Stately Studio.
173-
174-
First, select the state or event you want to tag.
175-
176-
#### On the canvas
177-
178-
1. Use the <Plus size={18} /> plus icon button to open the edit menu.
179-
2. Choose <Tag size={18} /> **Tag** from the menu to add a tag block.
180-
3. Write your tag’s name in the tag’s text input.
181-
4. Use the <PlusSquare size={18} /> plus icon button alongside your recent tag to add more tags.
182-
183-
#### Using the event details panel
184-
185-
1. Open the state or event <Info size={18} /> **Details** panel from the right tool menu.
186-
2. Use the **+ Tag** button to add a tag block.
187-
3. Write your tag’s name in the tag’s text input.
188-
4. Use the <PlusSquare size={18} /> plus icon button alongside your recent tag to add more tags.
170+
Read more about [tags](tags.mdx).
189171

190172
## Meta
191173

versioned_docs/version-5/guards.mdx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,31 @@ on: {
235235

236236
## TypeScript
237237

238-
_Coming soon_.
238+
You can strongly type the `guards` of your machine in the `types.guards` property of the machine config.
239+
240+
```ts
241+
const machine = createMachine({
242+
types: {} as {
243+
// highlight-start
244+
guards:
245+
| {
246+
type: 'isGreaterThan';
247+
params: {
248+
count: number;
249+
};
250+
}
251+
| { type: 'isAllowed' };
252+
// highlight-end
253+
},
254+
// ...
255+
on: {
256+
event: {
257+
guard: { type: 'isGreaterThan', params: { count: 5 } },
258+
// ...
259+
},
260+
},
261+
});
262+
```
239263

240264
## Cheatsheet
241265

versioned_docs/version-5/input.mdx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,16 +253,36 @@ const Component = (props) => {
253253

254254
## TypeScript
255255

256+
You can strongly type the `input` of your machine in the `types.input` property of the machine config.
257+
256258
```ts
257259
import { createActor, createMachine } from 'xstate';
258260

259261
const machine = createMachine({
260262
types: {} as {
263+
// highlight-start
261264
input: {
262265
userId: string;
263266
defaultRating: number;
264267
};
265-
// ...
268+
// highlight-end
269+
context: {
270+
userId: string;
271+
feedback: string;
272+
rating: number;
273+
};
274+
},
275+
context: ({ input }) => ({
276+
userId: input.userId,
277+
feedback: '',
278+
rating: input.defaultRating,
279+
}),
280+
});
281+
282+
const actor = createActor(machine, {
283+
input: {
284+
userId: '123',
285+
defaultRating: 5,
266286
},
267287
});
268288
```

versioned_docs/version-5/studio.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Stately Studio’s editor supports everything you need to visually build state m
3737
You can access quick start tutorials from the blue <HelpCircle size={12} /> Help button in the editor.
3838

3939
When you first visit the Editor, you’ll be shown two short videos (7m36s) as a quick start guide. You can access these videos again anytime from:
40+
4041
- Editor menu > Help > <GraduationCap size={12} /> **Learn Stately**.
4142
- The <HelpCircle size={12} /> Help button > <GraduationCap size={12} /> **Learn Stately**.
4243

0 commit comments

Comments
 (0)