RN Primitives Documentation

repository·main·Indexed 21 days ago

https://github.com/roninoss/rn-primitives

A suite of unstyled, accessible, and universal React Native components designed for high customizability. It provides functional logic and accessibility foundations for core UI elements—including Accordion, Alert Dialog, and Aspect-Ratio—while leaving visual styling to the developer. Supports both standard React Native StyleSheet patterns via @rn-primitives/expo-stylesheet and utility-first CSS frameworks like NativeWind.

Tokens
58.4K
Snippets
195
Records
309
Agent score
74%

What's inside RN Primitives

  1. What are React Native Primitives?

    main

    React Native Primitives provides unstyled, accessible building blocks for React Native applications. It offers a unified API that works across iOS, Android, and Web.

    Key characteristics include:

    • Truly Universal: Uses a single API for all platforms. The web implementation is powered by Radix UI, while native platforms use native implementations.
    • Accessible by Default: Includes built-in screen-reader support, focus management, and platform-correct semantics.
    • Unstyled: Components ship with zero styles, allowing you to integrate them into any design system without fighting existing CSS or style objects.
    • Flexible Integration: You can either install the components via npm packages or copy the source code directly into your project to have full control.
  2. Use Expo StyleSheet with RN Primitives

    main
    The @rn-primitives/expo-stylesheet package provides primitives that use the standard StyleSheet API from React Native instead of utility-first CSS frameworks like NativeWind. This is suitable for projects that prefer traditional React Native styling patterns over Tailwind-like classes.
  3. What are RN Primitives?

    main

    RN Primitives is a collection of universal, style-agnostic, and accessible React Native components.

    Key characteristics:

    • Unstyled Nature: Components are provided without predefined styles, giving developers full freedom to implement their own aesthetics and match their app's design system.
    • Accessibility: Components are designed and tested to align with accessibility standards, including appropriate labels, roles, and behaviors to ensure an inclusive user experience.
  4. Understand the Starlight project structure

    main

    A Starlight project follows a specific directory structure for content and assets:

    • src/content/docs/: The primary location for documentation. Starlight treats every .md or .mdx file in this directory as a route based on its filename.
    • src/assets/: Place images here to embed them in Markdown using relative links.
    • public/: Place static assets like favicons here.
    • astro.config.mjs: The main configuration file for Astro.
    • tailwind.config.mjs: Configuration for Tailwind CSS.
    .
    ├── public/
    ├── src/
    │   ├── assets/
    │   ├── content/
    │   │   ├── docs/
    │   │   └── config.ts
    │   └── env.d.ts
    ├── astro.config.mjs
    ├── package.json
    ├── tailwind.config.mjs
    └── tsconfig.json
  5. How to access Toggle Group context with hooks

    main

    If you are building custom components that need to interact with the Toggle Group state, use the provided context hooks:

    • useRootContext: Must be used within a Root component. It provides access to value, onValueChange, type, and disabled.
    • useItemContext: Must be used within a ToggleItem component. It provides access to the item's value.
  6. How Portals and PortalHosts work together

    main

    Portals allow you to render children into a different part of your application tree. The system relies on two main components:

    1. PortalHost: The destination where content will be rendered. By default, there is a single implicit host.
    2. Portal: The component that wraps the content you want to teleport.

    Default Behavior: If you don't specify a hostName on a <Portal />, its children will render into the default <PortalHost />.

  7. How Slot merges props with its child

    main

    When a prop exists on both the Slot and its child, they are combined according to these rules:

    PropBehavior
    Event handlers (e.g., onPress)Both run — the child's handler runs first, then the slot's.
    styleMerged into a single style array; the child's style wins in case of conflicts.
    classNameJoined together (slot's classes first, then the child's).
    refComposed — both the slot's ref and the child's ref receive the node.
    Everything elseThe child's value takes precedence.
  8. How to access Accordion context with hooks

    main

    You can use context hooks to access the state of the accordion or specific items within your custom components.

    • useRootContext: Must be used within a Root component. It provides access to type, disabled, collapsible, value, and onValueChange.
    • useItemContext: Must be used within an Item component. It provides access to value, disabled, and isExpanded.
  9. Manual installation of Avatar primitive

    main

    If you choose to install manually, you must copy the component code and its types to your local directory. Note that this primitive depends on @rn-primitives/types and @rn-primitives/slot. If these are not already in your project, you must add them first.

    1. Copy the component implementation to ~/components/primitives/avatar/index.tsx.
    2. Copy the type definitions to ~/components/primitives/avatar/types.ts.