Skip to content

@softr/sdk

The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.

region-visibility

62 linesSource

>-

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 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.

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