Toolbar
Touch-sized action toolbar with links, separators and toggle groups.
Install
First install? Set up the registry in your app.
npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add toolbarSource files
Expand a file to read the source the installer copies into your app.
components/ui/toolbar/Toolbar.tsx
import * as React from 'react'
import { cssInterop } from 'react-native-css-interop'
import * as Primitive from '@rn-primitives/toolbar'
import { renderTextChildren } from '@/lib/label-children'
import { cn } from '@/lib/utils'
export type ToolbarProps = Primitive.RootProps & { className?: string }
export const Toolbar = React.forwardRef<Primitive.RootRef, ToolbarProps>((props, ref) => (
<Primitive.Root
{...props}
ref={ref}
className={cn(
'flex-row flex-wrap items-center gap-[4px] rounded-lg border border-border bg-card p-[4px]',
props.className,
)}
/>
))
Toolbar.displayName = 'Toolbar'
export type ToolbarButtonProps = Omit<Primitive.ButtonProps, 'children'> & {
children?: React.ReactNode
className?: string
}
export const ToolbarButton = React.forwardRef<Primitive.ButtonRef, ToolbarButtonProps>(
({ children, className, ...props }, ref) => (
<Primitive.Button
{...props}
ref={ref}
className={cn(
'min-h-[48px] min-w-[48px] flex-row items-center justify-center gap-[8px] rounded-md px-[12px] py-[10px]',
props.disabled && 'opacity-50',
className,
)}
>
{renderTextChildren(children, 'font-medium')}
</Primitive.Button>
),
)
ToolbarButton.displayName = 'ToolbarButton'
export type ToolbarLinkProps = Omit<Primitive.LinkProps, 'children'> & {
children?: React.ReactNode
className?: string
}
export const ToolbarLink = React.forwardRef<Primitive.LinkRef, ToolbarLinkProps>(
({ children, className, ...props }, ref) => (
<Primitive.Link
{...props}
ref={ref}
className={cn(
'min-h-[48px] min-w-[48px] flex-row items-center justify-center gap-[8px] rounded-md px-[12px] py-[10px]',
className,
)}
>
{renderTextChildren(children, 'underline')}
</Primitive.Link>
),
)
ToolbarLink.displayName = 'ToolbarLink'
export type ToolbarSeparatorProps = Primitive.SeparatorProps & { className?: string }
export const ToolbarSeparator = React.forwardRef<Primitive.SeparatorRef, ToolbarSeparatorProps>((props, ref) => (
<Primitive.Separator
{...props}
ref={ref}
className={cn('mx-[4px] my-[8px] w-px self-stretch bg-border', props.className)}
/>
))
ToolbarSeparator.displayName = 'ToolbarSeparator'
const ToggleContext = React.createContext<string | string[] | undefined>(undefined)
export type ToolbarToggleGroupProps = Primitive.ToggleGroupProps & { className?: string }
export const ToolbarToggleGroup = React.forwardRef<Primitive.ToggleGroupRef, ToolbarToggleGroupProps>((props, ref) => (
<ToggleContext.Provider value={props.value}>
<Primitive.ToggleGroup {...props} ref={ref} className={cn('flex-row flex-wrap gap-[4px]', props.className)} />
</ToggleContext.Provider>
))
ToolbarToggleGroup.displayName = 'ToolbarToggleGroup'
export type ToolbarToggleItemProps = Omit<Primitive.ToggleItemProps, 'children'> & {
children?: React.ReactNode
className?: string
}
export const ToolbarToggleItem = React.forwardRef<Primitive.ToggleItemRef, ToolbarToggleItemProps>(
({ children, className, ...props }, ref) => {
const value = React.useContext(ToggleContext)
const selected = Array.isArray(value) ? value.includes(props.value) : value === props.value
return (
<Primitive.ToggleItem
{...props}
ref={ref}
className={cn(
'min-h-[48px] min-w-[48px] items-center justify-center rounded-md px-[12px] py-[10px]',
selected && 'bg-secondary',
props.disabled && 'opacity-50',
className,
)}
>
{renderTextChildren(children, 'font-medium')}
</Primitive.ToggleItem>
)
},
)
ToolbarToggleItem.displayName = 'ToolbarToggleItem'
// The published headless components forward className outside the NativeWind JSX transform.
cssInterop(Primitive.Root, { className: 'style' })
cssInterop(Primitive.Button, { className: 'style' })
cssInterop(Primitive.Link, { className: 'style' })
cssInterop(Primitive.Separator, { className: 'style' })
cssInterop(Primitive.ToggleGroup, { className: 'style' })
cssInterop(Primitive.ToggleItem, { className: 'style' })
components/ui/toolbar/index.ts
export * from './Toolbar'
Dependencies
Registry items
npm packages
react-native-css-interop@rn-primitives/toolbar