Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AGENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Use `docs/docs/` for the current Feliz 3 API. `docs/versioned_docs/version-2.9.0
- An `Html.*` list is either a list of children or a list of properties. When passing properties, place children inside `prop.children [...]`; use `prop.text` for simple text. Do not mix bare children with properties in the same list.
- Use `[<ReactComponent>]` on a function that returns a React element, then call it normally from parent F# code. Its parameters become React props. `[<ReactComponent(true)>]` additionally makes it the module's default export; it is not required for ordinary components. See `docs/docs/api-docs/feliz/react-component.mdx` and `passing-props.mdx`.
- Keep state and effects inside components with `React.useState`, `React.useEffect`, and the other React hooks. Use `React.useStateWithUpdater` when the next state must be computed from the previous state. Clean up subscriptions or timers in effects. See `docs/docs/api-docs/react/hooks/`.
- Some React 19 APIs have F#-specific names or shapes: `React.use'` (because `use` is an F# keyword; pass a promise created outside the component), `React.useAsyncTransition` for transitions that return a promise, `React.useActionState` with `prop.action` for form actions (the payload is `FormData`), and `React.ErrorBoundary`, which Feliz provides because React has no built-in error boundary component. New props for `React.ViewTransition` live in `viewTransition.*`.
- F# `if`, `match`, `for`, and `List.map` work in element construction. Give dynamic siblings stable `prop.key` values; default Feliz element creation may not produce React's missing-key warning. `Html.none` renders nothing. See `conditional-rendering.mdx`, `rendering-lists.mdx`, and `Html.mdx` under `docs/docs/api-docs/feliz/`.
- Mount the app with `ReactDOM.createRoot` and `root.render`. See `docs/docs/api-docs/react-dom/client-apis/createRoot.mdx`.

Expand Down
18 changes: 18 additions & 0 deletions docs/docs/api-docs/feliz/Html.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,24 @@ type onChange = File list -> unit
type onChange = DateTime -> unit
```

### Form actions

`prop.action` also takes a function, a React 19 [form action](../react/hooks/useActionState.mdx#form-actions). React calls it with the form's `FormData` on submit and resets the uncontrolled fields afterwards. The function can return `unit` or a `JS.Promise<unit>`:

```fsharp
Html.form [
prop.action (fun (formData: FormData) -> promise {
do! subscribe (unbox<string> (formData.get "email"))
})
prop.children [
Html.input [ prop.name "email"; prop.type'.email ]
Html.button [ prop.type'.submit; prop.text "Subscribe" ]
]
]
```

Use `prop.formAction` on a submit button to give it its own action.

### The empty element

To render the empty element, i.e. instructing React to render nothing, use `Html.none`
Expand Down
27 changes: 27 additions & 0 deletions docs/docs/api-docs/react-dom/apis/flushSync.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
title: flushSync
---

import ComponentRender from '@site/src/components/ComponentRender';
import ReactRefAdmonition from '@site/src/components/ReactRefAdmonition';

<ReactRefAdmonition href="https://react.dev/reference/react-dom/flushSync" name="flushSync"/>

---

`ReactDOM.flushSync` forces React to apply the state updates inside the callback synchronously, so the DOM is already updated when `flushSync` returns. It returns the result of the callback.

Use it sparingly: it can hurt performance. Typical reasons are reading the layout right after an update (e.g. to scroll to a new element), or browser APIs that expect the DOM to change synchronously, like `onbeforeprint`.

```fsharp
ReactDOM.flushSync (fun () -> setItems (items @ [ newItem ]))
// The new item is in the DOM now.
listRef.current |> Option.iter (fun list -> list.scrollTop <- list.scrollHeight)
```

import FlushSync from '../../../feliz-docs/fableoutput/Examples/ReactDOM/FlushSync'
import RawFlushSync from '!!raw-loader!../../../feliz-docs/Examples/ReactDOM/FlushSync.fs'

<ComponentRender code={RawFlushSync}>
<FlushSync />
</ComponentRender>
30 changes: 30 additions & 0 deletions docs/docs/api-docs/react-dom/apis/requestFormReset.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
title: requestFormReset
---

import ComponentRender from '@site/src/components/ComponentRender';
import ReactRefAdmonition from '@site/src/components/ReactRefAdmonition';

<ReactRefAdmonition href="https://react.dev/reference/react-dom/components/form" name="<form>"/>

---

`ReactDOM.requestFormReset` resets a form's uncontrolled fields to their default values once the current transition finishes.

React already does this after a [form action](../../react/hooks/useActionState.mdx#form-actions). Call it yourself when you handle the submission in `prop.onSubmit` but want the same reset behavior. It must be called inside a transition:

```fsharp
prop.onSubmit (fun ev ->
ev.preventDefault ()
let form = unbox<HTMLFormElement> ev.currentTarget
React.startTransition (fun () ->
saveNote form
ReactDOM.requestFormReset form))
```

import RequestFormReset from '../../../feliz-docs/fableoutput/Examples/ReactDOM/RequestFormReset'
import RawRequestFormReset from '!!raw-loader!../../../feliz-docs/Examples/ReactDOM/RequestFormReset.fs'

<ComponentRender code={RawRequestFormReset}>
<RequestFormReset />
</ComponentRender>
19 changes: 19 additions & 0 deletions docs/docs/api-docs/react-dom/client-apis/createRoot.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,22 @@ import CodeBlock from '@theme/CodeBlock';
<CodeBlock language="fsharp" >
{RawCreateRoot}
</CodeBlock>

## Root options

Pass `RootOptions` as the second argument to hook into React's error handling, or to prefix the IDs generated by `React.useId`:

| Option | Meaning |
| --- | --- |
| `onCaughtError` | Called when an [error boundary](../../react/components/ErrorBoundary.mdx) catches an error. |
| `onUncaughtError` | Called when an error is not caught by any error boundary. React unmounts the app in that case. |
| `onRecoverableError` | Called when React recovers from an error by itself, e.g. a hydration mismatch. |
| `identifierPrefix` | Prefix for `useId`. Avoids ID conflicts when several React roots share a page. |

The error callbacks receive the error and a `ReactErrorInfo` with the component stack. They are a good place to report errors to a logging service.

import RawCreateRootOptions from '!!raw-loader!../../../feliz-docs/Examples/ReactDOM/CreateRootOptions.fs'

<CodeBlock language="fsharp" >
{RawCreateRootOptions}
</CodeBlock>
10 changes: 10 additions & 0 deletions docs/docs/api-docs/react-dom/client-apis/hydrateRoot.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ import CodeBlock from '@theme/CodeBlock';
<CodeBlock language="fsharp" >
{RawHydrateRoot}
</CodeBlock>

`hydrateRoot` accepts the same `RootOptions` as [`createRoot`](./createRoot.mdx#root-options) as a third argument. If you set `identifierPrefix`, it must match the prefix used on the server. Hydration mismatches that React can recover from are reported to `onRecoverableError`.

```fsharp
ReactDOM.hydrateRoot (
document.getElementById "root",
App(),
RootOptions(onRecoverableError = fun error _ -> console.warn ("Hydration issue:", error.Message))
)
```
8 changes: 8 additions & 0 deletions docs/docs/api-docs/react-dom/hooks/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"label": "Hooks",
"position": 0,
"link": {
"type": "generated-index",
"title": "React-DOM hooks overview"
}
}
37 changes: 37 additions & 0 deletions docs/docs/api-docs/react-dom/hooks/useFormStatus.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
title: useFormStatus
---

import ComponentRender from '@site/src/components/ComponentRender';
import ReactRefAdmonition from '@site/src/components/ReactRefAdmonition';

<ReactRefAdmonition href="https://react.dev/reference/react-dom/hooks/useFormStatus" name="useFormStatus"/>

---

`ReactDOM.useFormStatus` returns the status of the last submission of the parent form. It must be called from a component rendered inside an `Html.form`, which makes it handy for reusable submit buttons that don't need any props.

The returned `FormStatus` has:

| Field | Meaning |
| --- | --- |
| `pending` | `true` while the parent form is submitting. |
| `data` | The submitted `FormData`, or `None` when there is no active submission. |
| `method` | `"get"` or `"post"`. |
| `action` | The function or URL passed to the parent form's `prop.action`. |

```fsharp
[<ReactComponent>]
let SubmitButton () =
let status = ReactDOM.useFormStatus ()
Html.button [ prop.type'.submit; prop.disabled status.pending; prop.text "Send" ]
```

`pending` only becomes `true` for [form actions](../../react/hooks/useActionState.mdx#form-actions) (`prop.action` with a function), not for forms handled with `prop.onSubmit`.

import UseFormStatus from '../../../feliz-docs/fableoutput/Examples/ReactDOM/UseFormStatus'
import RawUseFormStatus from '!!raw-loader!../../../feliz-docs/Examples/ReactDOM/UseFormStatus.fs'

<ComponentRender code={RawUseFormStatus}>
<UseFormStatus />
</ComponentRender>
40 changes: 40 additions & 0 deletions docs/docs/api-docs/react/apis/addTransitionType.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
title: addTransitionType
---

import ComponentRender from '@site/src/components/ComponentRender';
import ReactRefAdmonition from '@site/src/components/ReactRefAdmonition';

<ReactRefAdmonition href="https://react.dev/reference/react/addTransitionType" name="addTransitionType"/>

---

:::note

Requires React 19.3 or later.

:::

`React.addTransitionType` adds a type to the current transition, e.g. `"forward"` or `"backward"`. [`ViewTransition`](../components/ViewTransition.mdx) uses the types to pick an animation, and CSS can target them with `:active-view-transition-type(forward)`.

Call it inside `startTransition`, before the state update. Types are cleared after each commit.

```fsharp
React.startTransition (fun () ->
React.addTransitionType "forward"
setPage (page + 1))

React.ViewTransition [
viewTransition.update [ "forward", "feliz-forward"; "backward", "feliz-backward"; "default", "none" ]
viewTransition.children [ Html.h3 pages[page] ]
]
```

In the example below, "Next" slides the page in from the right and "Previous" from the left.

import AddTransitionType from '../../../feliz-docs/fableoutput/Examples/React/AddTransitionType'
import RawAddTransitionType from '!!raw-loader!../../../feliz-docs/Examples/React/AddTransitionType.fs'

<ComponentRender code={RawAddTransitionType}>
<AddTransitionType />
</ComponentRender>
35 changes: 31 additions & 4 deletions docs/docs/api-docs/react/apis/startTransition.mdx
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
---
title: ⚠️ startTransition
draft: true
title: startTransition
---

import NotImplemented from '@site/src/components/NotImplemented';
import ComponentRender from '@site/src/components/ComponentRender';
import ReactRefAdmonition from '@site/src/components/ReactRefAdmonition';

<NotImplemented href="https://react.dev/reference/react/startTransition" name="startTransition"/>
<ReactRefAdmonition href="https://react.dev/reference/react/startTransition" name="startTransition"/>

---

`React.startTransition` marks the state updates inside it as a non-blocking transition, like the function returned by [`useTransition`](../hooks/useTransition.mdx). It can be called anywhere, including outside of components (e.g. in a store), but it doesn't give you an `isPending` flag.

```fsharp
React.startTransition (fun () -> setQuery value)
```

It also accepts an async action. State updates after a `let!` need their own `startTransition`:

```fsharp
React.startTransition (fun () -> promise {
let! data = loadData ()
React.startTransition (fun () -> setData data)
})
```

In the example below, the input updates on every keystroke, while the slow list is updated in a transition.

import StartTransition from '../../../feliz-docs/fableoutput/Examples/React/StartTransition'
import RawStartTransition from '!!raw-loader!../../../feliz-docs/Examples/React/StartTransition.fs'

<ComponentRender code={RawStartTransition}>
<StartTransition />
</ComponentRender>

Inside a transition you can also call [`React.addTransitionType`](./addTransitionType.mdx) to choose a [`ViewTransition`](../components/ViewTransition.mdx) animation.
55 changes: 51 additions & 4 deletions docs/docs/api-docs/react/apis/use.mdx
Original file line number Diff line number Diff line change
@@ -1,9 +1,56 @@
---
title: ⚠️ use
draft: true
title: use
---

import NotImplemented from '@site/src/components/NotImplemented';
import ComponentRender from '@site/src/components/ComponentRender';
import ReactRefAdmonition from '@site/src/components/ReactRefAdmonition';

<NotImplemented href="https://react.dev/reference/react/use" name="use"/>
<ReactRefAdmonition href="https://react.dev/reference/react/use" name="use"/>

---

`use` reads the value of a promise or a context. In Feliz it is called `React.use'`, because `use` is a keyword in F#.

Unlike other hooks, `React.use'` can be called inside `if` expressions and loops.

## Reading a promise

`React.use'` suspends the component until the promise resolves. The nearest [`Suspense`](../components/Suspense.mdx) boundary shows its fallback in the meantime, and the nearest [`ErrorBoundary`](../components/ErrorBoundary.mdx) catches a rejected promise.

```fsharp
[<ReactComponent>]
let Quote (quotePromise: JS.Promise<string>) =
let quote = React.use' quotePromise
Html.p quote

React.Suspense([ Quote quotePromise ], Html.p "Loading quote...")
```

:::warning[Create the promise outside of the component that reads it]

A promise created during render is a new promise on every render, so the component suspends forever. Create it in an event handler, in a parent component, in a router loader or a cache, and pass it down.

For the same reason there is no `Async<'T>` overload: converting an `async` to a promise during render would start a new request on every render.

:::

import Use from '../../../feliz-docs/fableoutput/Examples/React/Use'
import RawUse from '!!raw-loader!../../../feliz-docs/Examples/React/Use.fs'

<ComponentRender code={RawUse}>
<Use />
</ComponentRender>

## Reading a context

`React.use'` also reads a context, like [`useContext`](../hooks/useContext.mdx), but it may be called conditionally:

```fsharp
[<ReactComponent>]
let Heading (showTheme: bool) =
if showTheme then
let theme = React.use' ThemeContext
Html.h2 $"Theme: {theme}"
else
Html.h2 "Hello"
```
38 changes: 38 additions & 0 deletions docs/docs/api-docs/react/components/Activity.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
title: Activity
---

import ComponentRender from '@site/src/components/ComponentRender';
import ReactRefAdmonition from '@site/src/components/ReactRefAdmonition';

<ReactRefAdmonition href="https://react.dev/reference/react/Activity" name="Activity"/>

---

:::note

Requires React 19.2 or later.

:::

`React.Activity` hides and shows part of the UI while keeping its state. With `ActivityMode.Hidden`, React hides the children with `display: none`, cleans up their effects and renders updates for them at low priority. Switching back to `ActivityMode.Visible` restores the previous state and re-runs the effects.

That makes it a good fit for tabs, sidebars or content you want to pre-render before the user navigates to it.

```fsharp
React.Activity(
(if isOpen then ActivityMode.Visible else ActivityMode.Hidden),
Sidebar()
)
```

In the example below, type into both text areas and hide them. The one inside `Activity` keeps its text; the one rendered with `if` is unmounted and loses it.

import Activity from '../../../feliz-docs/fableoutput/Examples/React/Activity'
import RawActivity from '!!raw-loader!../../../feliz-docs/Examples/React/Activity.fs'

<ComponentRender code={RawActivity}>
<Activity />
</ComponentRender>

`React.Activity` accepts a single child or a list of children.
Loading