@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
62 lines (43 loc) • 2.61 kB
Markdown
name: region-visibility
description: >-
Show part of a page only to the visitors a rule admits: the Audience
component, its children and fallback, where a region's rule may be declared,
and why hiding a control is not authorization. Use when a button, a column, a
nav item or a panel should appear for some visitors and not others, on a page
everyone may open. Gating the whole route is routes-and-visibility.
metadata:
type: sub-skill
library: softr-sdk
library_version: 1.19.0
**Boundary.** `Audience` and `defineVisibility` come from `@softr/sdk` and work anywhere.
# Region visibility
A route's rule decides who may open the page. A region's rule decides what they see once they are on it. The two are independent: an admin panel on a page every signed-in visitor may open is a region, and so is a refund button only staff should reach for.
## Setup
Wrap the region in `Audience` and give it a rule:
```tsx
import { Audience, defineVisibility } from '@softr/sdk'
const staffOnly = defineVisibility({
userGroupTypes: ['LOGGED_IN_USERS'],
customGroups: ['00000000-0000-0000-0000-00000000000a'],
})
function InvoiceActions({ invoice }: { invoice: Invoice }) {
return (
<Audience visibility={staffOnly} fallback={<p>Contact your account manager to refund.</p>}>
<RefundButton invoice={invoice} />
</Audience>
)
}
```
`Audience` renders its children for the visitors the rule admits and `fallback` for everyone else. Without a `fallback` it renders nothing.
The rules are the same four a route uses, and [routes-and-visibility](../routes-and-visibility/SKILL.md) covers them.
## Core Patterns
### Where the rule is declared
A region's rule is an ordinary value. It can sit beside the component, in the route file, or in a module the whole app shares so several regions read the same one.
It is separate from the route's own rule. That one is the `visibility` of the route's `definePage`, which `beforeLoad` applies: see [routes-and-visibility/SKILL.md](../routes-and-visibility/SKILL.md).
### Hiding a control is not authorization
`Audience` decides what to render and nothing else. The server function behind a hidden button is still reachable on its own, and Softr authorizes that request whatever the page rendered. If a region hides something sensitive, declare the action behind it with the permissions it should have.
## References
- [routes-and-visibility/SKILL.md](../routes-and-visibility/SKILL.md): the rule shapes, and gating a whole page
- [current-user/SKILL.md](../current-user/SKILL.md): reading the visitor a rule has admitted