Dripsy Documentation

repository·master·Indexed 24 days ago

https://github.com/nandorojo/dripsy

A library of unstyled, responsive UI primitives for React Native and Web (version 3.4.0). Dripsy provides a high-performance, theme-based styling system using an `sx` prop, supporting Expo, Vanilla React Native, and Next.js. Key features include universal support, responsive design, dark mode, and a `styled()` function for creating theme-aware components. It includes utilities like `makeTheme` for TypeScript intellisense, `useDripsyTheme` and `useSx` hooks, and comprehensive support for custom font configuration.

Tokens
19.4K
Snippets
69
Records
122
Agent score
80%

What's inside Dripsy

  1. Overview of Dripsy

    master

    Dripsy provides unstyled, responsive UI primitives designed for universal applications running on React Native and Web. It allows you to define themed, responsive designs using a single sx prop, which is memoized under the hood for performance. It is compatible with Expo, Vanilla React Native, and Next.js.

    <View sx={{ bg: '$primary', height: [100, 200] }} />
  2. Dripsy core features and compatibility

    master

    Dripsy provides unstyled, responsive UI primitives for universal React applications. Key features include:

    • Responsive Styles: Use arrays or objects in the sx prop.
    • Universal Support: Works on Android, iOS, Web, and more.
    • Ecosystem Compatibility: Works with Expo, Vanilla React Native, Next.js, Animated, Reanimated, Moti, and @expo/vector-icons.
    • Theming: Full theme support including custom theme variants and dark mode / custom color modes.
    • Performance: sx prop is memoized; uses Atomic CSS classes with StyleSheet.create under the hood.
    • Advanced Styling: Support for Linear Gradient and custom fonts.
  3. Key Features of Dripsy

    master

    Dripsy is designed for high-performance, universal styling with the following capabilities:

    • Universal Support: Works with Expo, Vanilla React Native, and Next.js.
    • Responsive Design: Supports responsive styles (e.g., using arrays for different breakpoints).
    • Theming: Full theme support including custom theme variants and global font editing.
    • Performance: The sx prop is memoized automatically, even when written inline. It uses atomic CSS classes on the web while using StyleSheet.create under the hood.
    • Color Modes: Built-in support for dark mode and custom color modes.
    • Compatibility: Works with Animated, Reanimated, Moti, @expo/vector-icons, and Linear Gradient.
  4. Implement incremental strict types per scale

    master

    Instead of enforcing strict types globally, you can apply them to specific scales (like space, colors, etc.) by passing an object to onlyAllowThemeValues.

    To get the best experience with strict types, it is recommended to use a dictionary with $ prefixes for your scales instead of arrays. This allows TypeScript to provide precise errors and makes refactoring theme structures much easier.

    Example of a scale-specific strict configuration:

    // ✅ Valid: uses theme key
    <View sx={{ padding: '$1' }} />
    
    // ❌ Invalid: uses raw value
    <View sx={{ padding: 10  }} />
    const theme = makeTheme({
      space: {
        $0: 0,
        $1: 4,
        $2: 8,
        $3: 16,
        $4: 32,
        $5: 64,
        $6: 128,
        $7: 256,
        $8: 512,
      },
      types: {
        onlyAllowThemeValues: {
          space: 'always',
        },
      },
    })
  5. Alias font weights in the Dripsy theme

    master

    You can create semantic aliases for font weights using the fontWeights key in your theme. This allows you to use names like black instead of numeric strings like '900'. The value of the alias must correspond to a weight defined in your customFonts configuration.

    Once aliased, you can use these names in the sx prop of components or within theme.text variants.

  6. Understand the Dripsy theme specification

    master
    Dripsy's theme structure closely follows the theme-ui theme specification. While most concepts from theme-ui apply, there are slight divergences in how certain properties are handled, often due to the differences between React Native and standard CSS or to provide improved APIs. For the core specification, you can refer to the theme-ui documentation, but keep in mind Dripsy-specific implementation details.
  7. Implement Color Modes in Dripsy

    master

    Unlike Theme UI, Dripsy does not use a colors.modes object within a single theme. Instead, Dripsy uses a React-based approach where you switch the entire theme object passed to the DripsyProvider based on the current color scheme.

    To implement this, you should:

    1. Create a base theme using makeTheme to define your primary color schema.
    2. Create additional theme objects (e.g., themeLight) that match the shape of your base theme.
    3. Use a hook like useColorScheme from react-native (or a custom context for user-defined preferences) to detect the mode.
    4. Pass the appropriate theme to DripsyProvider based on that detection.
    const colorMode = useColorScheme()
    
    return (
      <DripsyProvider theme={colorMode === 'dark' ? theme : themeLight}>
        <App />
      </DripsyProvider>
    )
  8. How custom fonts work in Dripsy

    master
    While font aliasing follows theme-ui patterns, implementing actual custom font files is specific to the React Native environment. Because Dripsy is designed for cross-platform use (Web and React Native), the mechanism for loading and applying custom font assets must account for React Native's asset handling.
  9. How strict variants work

    master

    Under strict variant mode (the default):

    1. Components with a themeKey: A component is restricted to variants defined under its specific theme key. For example, a <Text /> component can only use variants found in theme.text. Using any other variant will result in a TypeScript error.
    2. Components without a themeKey: Components like View that do not have a defined themeKey are permitted to use any nested variant from anywhere in your theme.
  10. Use the `variant` and `variants` props

    master

    Dripsy components support a variant prop to apply a single style preset from your theme. Unlike Theme UI, Dripsy also supports the variants prop, which accepts an array of strings to apply multiple ordered variants to a single component.

    • variant: Applies a single variant from the component's associated themeKey.
    • variants: Applies multiple variants in the order they appear in the array.
  11. How responsive arrays work in Dripsy

    master

    To implement responsive design without manual Dimensions listeners or useState hooks, use an array in the sx prop. Each element in the array applies to a specific breakpoint.

    For example, to change a View width from 100% on mobile to 50% on larger screens:

    import { View } from 'dripsy'
    
    const ResponsiveBox = () => {
      return <View sx={{ width: ['100%', '50%'] }} />
    }