SwiftUI.js
Components

NavigationBar

NavigationBar is a component that displays a title, optional back button, and toolbar items. It's automatically used by StandardPage when a `navigationTitle` is provided, following iOS Human Interface Guidelines and SwiftUI's navigation bar design patterns.

NavigationBar is a component that displays a title, optional back button, and toolbar items. It's automatically used by StandardPage when a `navigationTitle` is provided, following iOS Human Interface Guidelines and SwiftUI's navigation bar design patterns.

Examples

Basic Usage

Preview unavailable for this example in the static docs build.

Show code
<NavigationBar title="Home" />

Automatic Back Button

Preview unavailable for this example in the static docs build.

Show code
<NavigationBar 
      title="Details" 
      showBackButton={true}
      onBack={() => console.log('Back clicked')}
    />

With Toolbar Items

Preview unavailable for this example in the static docs build.

Show code
<NavigationBar 
      title="Product Details"
      showBackButton={true}
      onBack={() => console.log('Back clicked')}
      toolbarItems={
        <HStack spacing={8}>
          <Button>Share</Button>
          <Button>Edit</Button>
        </HStack>
      }
    />

Integration with NavigationStack

Preview unavailable for this example in the static docs build.

Show code
<NavigationStack>
        <VStack spacing={20}>
          <NavigationBar title="Home" />
          <Text>Home Page Content</Text>
        </VStack>
      </NavigationStack>

API

PropTypeRequiredDescription
titlestringNoNavigation bar title
showBackButtonbooleanNoWhether to show back button Default: false
onBack() => voidNoCallback when back button is clicked
toolbarItemsReactNodeNoToolbar items to display on the right side

Overview

NavigationBar is a component that displays a title, optional back button, and toolbar items. It's automatically used by StandardPage when a navigationTitle is provided, following iOS Human Interface Guidelines and SwiftUI's navigation bar design patterns.

SwiftUI Correspondence: Similar to SwiftUI's .navigationTitle() and .toolbar() modifiers.

Basic Usage

NavigationBar is typically used automatically through StandardPage's navigationTitle prop:

NavigationBar can also be used directly:

Automatic Back Button

The back button automatically appears when the page is not the root page:

With Toolbar Items

Add toolbar items on the right side of the navigation bar:

Single Toolbar Button

<StandardPage
  id="edit"
  navigationTitle="Edit Profile"
  toolbarItems={<Button onClick={handleSave}>Save</Button>}
>
  <Form>...</Form>
</StandardPage>

Custom Back Button Behavior

The back button automatically calls dismiss() from the navigation context. If you need custom behavior, you can access the navigation context:

import { useNaviContext } from '@swiftuijs/ui'

function CustomPage() {
  const navi = useNaviContext()

  const handleCustomBack = () => {
    // Custom logic before going back
    if (hasUnsavedChanges) {
      showConfirmDialog(() => navi.dismiss())
    } else {
      navi.dismiss()
    }
  }

  return (
    <StandardPage
      id="custom"
      navigationTitle="Custom Page"
      // Note: Back button behavior is handled automatically
      // For custom behavior, you might need to hide the default
      // and add a custom button in toolbarItems
    >
      ...
    </StandardPage>
  )
}

Integration with NavigationStack

NavigationBar works seamlessly with NavigationStack:

Props Reference

StandardPage Props (for NavigationBar)

PropTypeDefaultDescription
navigationTitlestring-Title to display in navigation bar. When provided, navigation bar is shown.
toolbarItemsReactNode-Items to display on the right side of navigation bar.

NavigationBar is used internally, but you can access it directly if needed:

PropTypeDefaultDescription
titlestring-Navigation bar title
showBackButtonbooleanfalseWhether to show back button
onBack() => void-Callback when back button is clicked
toolbarItemsReactNode-Toolbar items to display

Common Patterns

Master-Detail Pattern

// Master (List)
<StandardPage id="products" navigationTitle="Products">
  <List>
    {products.map(product => (
      <NavigationLink
        key={product.id}
        destination={() => <ProductDetail product={product} />}
        pageOptions={{ type: 'page' }}
      >
        <ProductRow product={product} />
      </NavigationLink>
    ))}
  </List>
</StandardPage>

// Detail
function ProductDetail({ product }) {
  return (
    <StandardPage
      id={`product-${product.id}`}
      navigationTitle={product.name}
      toolbarItems={<Button>Share</Button>}
    >
      <VStack>
        <Image src={product.image} />
        <Text>{product.description}</Text>
      </VStack>
    </StandardPage>
  )
}

Form with Save Button

<StandardPage
  id="edit"
  navigationTitle="Edit Profile"
  toolbarItems={
    <Button
      onClick={handleSave}
      disabled={!isValid}
    >
      Save
    </Button>
  }
>
  <Form onSubmit={handleSubmit}>
    <TextField label="Name" value={name} onChange={setName} />
    <TextField label="Email" value={email} onChange={setEmail} />
  </Form>
</StandardPage>

Multiple Toolbar Actions

<StandardPage
  id="article"
  navigationTitle="Article"
  toolbarItems={
    <HStack spacing={12}>
      <Button onClick={handleBookmark}>
        <Text>🔖</Text>
      </Button>
      <Button onClick={handleShare}>
        <Text>Share</Text>
      </Button>
    </HStack>
  }
>
  <ScrollView>
    <Text>{articleContent}</Text>
  </ScrollView>
</StandardPage>

Styling

NavigationBar uses CSS variables for theming. You can customize colors:

:root {
  --sw-color-blue: #007AFF; /* Back button color */
  --sw-color-background-primary: #FFFFFF; /* Navigation bar background */
  --sw-color-separator: #C6C6C8; /* Bottom border */
}

Best Practices

  1. Keep titles concise: Navigation bar titles should be short and descriptive
  2. Use toolbar for actions: Place action buttons in toolbarItems, not in page content
  3. Limit toolbar items: Too many toolbar items can clutter the navigation bar
  4. Consistent navigation: Use navigationTitle consistently across related pages
  5. Accessibility: Ensure toolbar buttons have proper labels for screen readers

On this page