Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
đˇ A super-simple responsive design system for React Native Web.
A dead-simple, responsive design system for Expo / React Native Web. Heavily inspired by React's theme-ui
.
Style once, run anywhere.
<Text
sx={{
fontSize: [14, 16, 20], // 14 on mobile, 16 on tablet, 20 on desktop
color: ['primary', null, 'accent'], // `primary` on mobile & tablet, `accent` on desktop
}}
>
Responsive font size?? đ¤¯
</Text>
Build once, deploy everywhere, is a great philosophy made possible by Expo Web/React Native Web. A large impediment is responsive design.
React Native doesn't have media queries for styles, and trying to micmick it with JS turns into useState
hell with a ton of conditionals (as you'll see below.)
While React Native has some great component libraries, it lacks a good design system that is responsive and themed.
No longer. The goal of Dripsy is to let you go from idea to universal, themed styles without much effort.
There is no shortage of discussion about what responsive design should look like in React Native.
After trying many, many different ways, I'm convinced this approach is the answer. I'm curious to see if you'll think the same.
yarn add dripsy
# or
npm i dripsy
If you're using Next.js or another SSR app, scroll down to see how to configure.
Technically, you don't have to do anything else!
However, you'll likely want to create a custom theme.
Wrap your entire app with the DripsyProvider
, and pass it a theme
object. Make sure you create your theme outside of the component to avoid re-renders.
If you're using Next.js, this goes in pages/_app.js
.
App.js
import { DripsyProvider } from 'dripsy'
const theme = {
colors: {
text: '#000',
background: '#fff',
primary: 'tomato',
},
fonts: {
body: 'system-ui, sans-serif',
heading: '"Avenir Next", sans-serif',
},
spacing: [10, 12, 14],
}
export default function App() {
return (
<DripsyProvider theme={theme}>
{/* Your app code goes here! */}
</DripsyProvider>
)
}
Follow the docs from theme-ui
to see how to theme your app â we use the same API as them.
My personal preference is to have the entire theme object in one file.
All theme values are optional. You don't have to use them if you don't want.
If you are not using Next.js, skip down to #3 below.
Steps 1 & 2 are required for Next.js apps (for example, if you're using Expo + Next.js.)
1. Install dependencies
yarn add next-compose-plugins next-transpile-modules
2. Edit your next.config.js
file to look like this:
const withPlugins = require('next-compose-plugins')
const withTM = require('next-transpile-modules')([
'dripsy',
// you can add other packages here that need transpiling
])
const { withExpo } = require('@expo/next-adapter')
module.exports = withPlugins(
[withTM],
withExpo({
projectRoot: __dirname,
})
)
SSRStyleReset
to the top of your body
Import SSRStyleReset
and inject it at the top of your body
HTML tag.
import { SSRStyleReset } from 'dripsy'
<body>
<SSRStyleReset />
<YourApp />
</body>
If you're using Next.js, this should go in pages/_document.js
.
Your pages/_document.js
should look something like this.
We'll add other library examples here too, such as Gatsby.
That's it! Btw, if you're using Expo + Next.js, check out my library, expo-next-react-navigation to help with navigation.
export default {
colors: {
text: '#000',
background: '#fff',
primary: 'tomato',
},
spacing: [10, 12, 14],
fontSizes: [16, 20, 24],
text: {
h1: {
fontSize: 3, // this is 24px, taken from `fontSize` above
},
p: {
fontSize: 1, // & this is 16px, taken from `fontSize` above
},
},
}
<Text
sx={{
color: 'primary',
padding: [1, 3], // [10px, 14px] from theme!
}}
>
Themed color!
</Text>
import { H1, H2, P } from 'dripsy'
<H1
sx={{
color: 'text', // #000 from theme!
fontSize: 2 // 24px from theme!
}}
>
</H1>
Credit to Evan Bacon for @expo/html-elements, used above!
Todo: make the theme values show up in TS types for intelliesense.
Dripsy is an almost-drop-in replacement for React Native's UI components.
Change your imports from react-native
to dripsy
- import { View, Text } from 'react-native'
+ import { View, H1, P } from 'dripsy'
Use the sx
prop instead of style
to use themed and responsive styles:
<View
sx={{
height: [100, 400],
backgroundColor: 'primary',
marginX: 10,
}}
/>
Also, instead of marginHorizontal
, use marginX
or mx
, as seen on the theme-ui
docs.
To use an animated view, simple use the as
prop.
import { View } from 'dripsy'
import Animated from 'react-native-reanimated'
import { useValue } from 'react-native-redash'
function App() {
const height = useValue(0)
return <View as={Animated.View} sx={{ height }} />
}
This is what it took to make one responsive style without Dripsy...
import { useState } from 'react'
import { View } from 'react-native'
const ResponsiveBox = () => {
const [screenWidth, setScreenWidth] = useState(Dimensions.get('window').width)
useEffect(() => {
const onResize = event => {
setScreenWidth(event.window.width)
}
Dimensions.addEventListener('change', onResize)
return () => Dimensions.removeEventListener('change', onResize)
}, [])
let width = '100%'
if (screenWidth > 700) {
width = '50%'
}
return <View style={{ width }} />
}
A big issue with using JS-only breakpoints like that is that it won't work on SSR apps using Expo + Next.js. The "solution" would be to lazy load the component, but then you lose the SEO benefits of Next.js. With Dripsy, SSR works fine!
import { View } from 'dripsy'
const ResponsiveBox = () => {
return <View sx={{ width: ['100%', '50%'] }} />
}
đ¨ More docs coming here!!!
styled
import { View } from 'react-native'
import { styled } from 'dripsy'
const StyledView = styled(View)({
flex: 1,
bg: 'primary'
})
// This uses the theme.layout.container styles!
const StyledView2 = styled(View, { themeKey: 'layout', defaultVariant: 'container' })({
flex: 1,
bg: 'primary'
})
createThemedComponent
Prefer
styled
.
Currently, a bunch of the React Native components are supported. That said, I haven't added them all. If you want to add one, go to src/components
and add one and submit a PR.
Or, you can use the createThemedComponent
function in your own app.
import { createThemedComponent } from 'dripsy'
import { View } from 'react-native'
const CustomView = createThemedComponent(View, {
defaultStyle: {
flex: 1,
},
})
First, this library is super inspired by theme-ui
, and uses many of its low-level functions and methodologies.
Practically speaking, this library uses the Dimensions
api on Android & iOS, and uses actual CSS breakpoints on web. The CSS breakpoints are made possible by @artsy/fresnel
. This means that you get actually-native web breakpoints. That matters, because server-size rendered apps will have startup issues if you use JS-based media queries that require React to rehydrate on when it opens.
On Native, there is nothing too fancy going on. We track the screen width, generate styles based on the current width using a mobile-first approach, and return the regular React Native components. But it just feels like magic!
This is a really new project. I'd love your help and contributions.
MIT
FAQs
đˇ A super-simple responsive design system for React Native Web.
The npm package dripsy receives a total of 3,988 weekly downloads. As such, dripsy popularity was classified as popular.
We found that dripsy demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.