Tamagui Implementation Guide for Daggh Companion
Overview
This document provides a technical summary of how Tamagui is integrated into the Daggh Companion project, including setup, best practices, common pitfalls, and solutions. It is intended for developers working on or maintaining the codebase.
Project Structure Reference
- Tamagui config:
daggh-companion/tamagui.config.ts - Provider wrapper:
daggh-companion/providers/TamaguiProviderWrapper.tsx - Babel config:
daggh-companion/babel.config.js - Theme usage example:
daggh-companion/app/(auth)/login.tsx
Key Setup Steps
-
Tamagui Configuration
- The main config is in
tamagui.config.tsand usescreateTamagui()to extend the default config and add custom themes (brand,brand_dark). - Export the result of
createTamagui()as the default export. - Example:
import { createTamagui } from 'tamagui';
import { defaultConfig } from '@tamagui/config/v4';
// ...define custom themes...
const config = { ... };
export default createTamagui(config);
- The main config is in
-
Provider Setup
- The app is wrapped in
TamaguiProviderWrapper, which imports the config and provides it to the app viaTamaguiProvider. - This ensures all Tamagui components have access to the theme and tokens.
- The app is wrapped in
-
Babel Plugin Configuration
- The Tamagui Babel plugin is required for token and theme compilation.
- In
babel.config.js, ensure:plugins: [
['@tamagui/babel-plugin', {
components: ['tamagui'],
config: './tamagui.config.ts',
logTimings: true,
disableExtraction: process.env.NODE_ENV === 'development',
}],
...
] - Set
process.env.TAMAGUI_TARGET = 'native'at the top of the file.
-
Expo/Metro
- No custom
metro.config.jsis required unless you have special needs. - Always run with a custom dev client (not Expo Go) for Tamagui native support.
- No custom
Theme & Token Usage
- Use
bgorbackgroundprops for background colors:<Button bg="$primary">Primary</Button> - Avoid
backgroundColoras it is not type-safe in Tamagui and will cause TypeScript errors. - Use theme tokens with a
$prefix (e.g.,$primary,$secondary). - To apply a custom theme, use the
themeprop:<YStack theme="brand">...</YStack>
Common Pitfalls & Solutions
1. TypeScript Errors with backgroundColor
- Problem:
backgroundColoris not a valid prop in Tamagui's type definitions. - Solution: Use
backgroundorbginstead.
2. Custom Theme Tokens Not Available
- Problem: Custom tokens (e.g.,
primary,secondary) are not accessible in components. - Solution:
- Ensure your custom theme is set as the default in the config (
defaultTheme: 'brand'). - Use the
themeprop on a parent component if you want to scope a subtree to a custom theme.
- Ensure your custom theme is set as the default in the config (
3. Styles Not Applying or Tokens Not Working
- Problem: Styles or tokens do not work as expected.
- Solution:
- Ensure the Tamagui Babel plugin is configured and running (look for Tamagui logs in the terminal).
- Clear Metro and Babel caches:
expo start -c. - Do not use Expo Go; use a custom dev client.
4. Config Import Issues
- Problem: Importing the config incorrectly (e.g., using a default import for a named export).
- Solution: Always use the correct import/export style. For Tamagui config, export the result of
createTamagui()as default.
References & Further Reading
Summary Checklist
- Use
background/bgfor backgrounds, notbackgroundColor - Use theme tokens with
$prefix - Wrap app in
TamaguiProviderwith your config - Use the Tamagui Babel plugin and set
TAMAGUI_TARGET - Run with a custom dev client, not Expo Go
- Clear caches if styles/tokens are not working
For more details, see the referenced docs and the project structure above.
Tamagui Config (tamagui.config.ts)
- The config file is the heart of your design system. It defines tokens, themes, fonts, and more.
- Extending Themes:
- To add custom themes (e.g.,
brand,brand_dark), extend the base themes fromdefaultConfig.themes.lightanddefaultConfig.themes.dark. - Example:
const brand = {
...defaultConfig.themes.light,
background: "#18181b",
color: "#fff",
primary: "#6366f1",
// ...
}
- To add custom themes (e.g.,
- Adding Tokens:
- You can add or override tokens (e.g.,
radius,space) by spreading and extendingdefaultConfig.tokens.
- You can add or override tokens (e.g.,
- Setting Defaults:
- Set
defaultThemeanddefaultTheme_darkto your custom themes to make them the app-wide default.
- Set
- Export:
- Always export the result of
createTamagui(config)as the default export.
- Always export the result of
- Common Mistakes:
- Forgetting to spread the base theme (
...defaultConfig.themes.light) can cause missing keys and runtime errors. - Not exporting the result of
createTamagui()will break the provider. - Using a named export instead of default can cause import issues in the provider.
- Forgetting to spread the base theme (
- TypeScript Integration:
- Use the
declare module "tamagui"block to extend Tamagui's types with your config.
- Use the
See the actual config in daggh-companion/tamagui.config.ts for a working example.