How to build a universal component
Building in a universal way means building for both mobile and web by sharing the same source code, or at least partially. Sharing more code between mobile and web results in less maintenance and more consistency between platforms. Therefore, it's strongly encouraged, but there are some things to know when building a universal component.
How it works
Universal components are built with React Native and React Native Web. This means it's a mobile component that can be used on web. You can read more why universal here.
Things to know
- We are building first for mobile, then for web. That's why we have
.web.tsxfiles andisWebcondition. - Use only universal components. You can check if a component is universal by looking at the documentation.
- SX and style are the only ways to style your components. You can't use
classNameand css global styles. - HTML elements will break on mobile, you have to use
Boxinstead ofdiv, for example. - Web specific props like
classNameare ignored on mobile. As such, you can't rely on Emotion'scssprop
Available tools
Tamagui Core
We use Tamagui Core to build our root componennts like <Box /> but also to setup the theme and mediaQueries.
We also rely on it to display popovers and tooltip. Don't hesitate to check the documentation to see what's available.
⚠️ But we don't use styled components from Tamagui like Button, Radio...
SX prop
The go-to property to style your component with the theme and mediaQueries built in. You can find a detailed guide here.
isWeb condition
You can use the isWeb condition to create alternative behavior for web and mobile. This can be useful to adjust very specific use cases.
👉 But don't use it at the root of your component to switch between an entire mobile and web component. Use .native files instead, see below.
import { isWeb } from "murray";
const MyComponent = () => {
return <Text size={isWeb ? "$size300" : "$size400"}>Some text</Text>;
};
.web.tsx files
On rare occasions, you may be forced to create a .web.tsx file to manage universality. This file will be used only on web.
👉 In this case, we recommend you to split your component into small universal components and use a .web.tsx file to compose them.
⚠ Typescript limitation: your component prop types should be the same on mobile and web. If you have a prop that is only used on web, you can just ignore it on mobile and mark it as @web only. We also recommend you set your types in a common file to avoid type drift in the future.
// MyComponent/MyComponentProps.ts
export interface MyComponentProps {
// @web only
webOnlyProp?: string;
otherProps?: string;
}
// MyComponent/MyComponent.tsx
import { View } from "react-native";
import { AUniversalSubComponent } from "./AUniversalSubComponent";
import { type MyComponentProps } from "./MyComponentProps";
const MyComponent = ({ ...props }: MyComponentProps) => {
return (
<View>
<AUniversalSubComponent />
</View>
);
};
// MyComponent/MyComponent.web.tsx
import { AUniversalSubComponent } from "./AUniversalSubComponent";
import { type MyComponentProps } from "./MyComponentProps";
const MyComponent = ({ ...props }: MyComponentProps) => {
return (
<div>
<AUniversalSubComponent />
</div>
);
};
.native.tsx files
Contrarily, .native.tsx files also work. We recommend you to first build for mobile and use .web.tsx to bring specificities for the web. However, if needed, this file will be used only on mobile.
Getting inspired
You can get inspired by looking at the existing universal components. Here are some examples:
- Timeline is using
isWebcondition to change the way to render the line between items - ContainerNext is universal and handles responsivness for both web and mobile at the same time