question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Margin and Padding lower than 9px powers its value when using sx or styled

See original GitHub issue

In our project, we’re using dripsy within a styles.ts file theming everything with styled similar to styled-components. ex:

import { styled } from 'dripsy'
import { Text, View } from 'react-native'

export const Container = styled(View)((_props) => ({
  justifyContent: 'center',
  alignItems: 'center',
  backgroundColor: '#e03d26',
  borderRadius: 5,
}))

But when I set margin or padding the final code get it’s value powered (happens on Next and Expo), examples:

Example 1

Padding with 8px using styled()

Code (8px):

export const Container = styled(View)((_props) => ({
  justifyContent: 'center',
  alignItems: 'center',
  backgroundColor: '#e03d26',
  borderRadius: 5,
  padding: 8
}))

Browser (512px which is 2⁹): image

Example 2

Padding with 7px using dripsy <View/> with sx:

Code (7px):

import { View } from 'dripsy'
import { Title } from './styles'

const Button = ({ title }: { title: string }) => {
  return (
    <View
      sx={{
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#e03d26',
        borderRadius: 5,
        padding: 7,
      }}
    >
      <Title>{title}</Title>
    </View>
  )
}

Browser (256px wich is 2⁸) image

Example 3 - Works as Expected

Padding with 9px using styled()

Code(9px):

export const Container = styled(View)((_props) => ({
  justifyContent: 'center',
  alignItems: 'center',
  backgroundColor: '#e03d26',
  borderRadius: 5,
  padding: 9
}))

Browser (9px): image

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
nandorojocommented, Apr 6, 2022

Oof, I see the issue:

Screen Shot 2022-04-06 at 5 16 47 PM

Looks like we set the default space for you. So you can fix it with the comment above. I think this code is pulled over from theme-ui, and since everyone always overrides it, it’s never been an issue until now.

It would be a breaking change to remove this, but I can do it in the next release. For now, if you explicitly set space in your theme, it will solve this.

2reactions
nandorojocommented, Apr 6, 2022

I recommend doing this:

const theme = makeTheme({
  space: {
    $0: 0,
    $1: 4,
    $2: 8,
    $3: 16,
    $4: 32,
    $5: 64,
    $6: 128,
    $7: 256,
    $8: 512
  }
})

And then:

<View sx={{ pr: '$3' }} />
Read more comments on GitHub >

github_iconTop Results From Across the Web

The sx prop - MUI System
The sx prop is a shortcut for defining custom styles that has access to the theme. The sx prop lets you work with...
Read more >
The `sx` Prop - Theme UI
Margin and padding are treated as first-class citizens in Theme UI, using values defined in the theme.space scale, and include an optional shorthand...
Read more >
MUI: how to style a <p> using sx prop? - Stack Overflow
When using MUI v5's styling system, when you write ... to <p> elements having a margin-bottom, because p is used as a shorthand...
Read more >
All You Need To Know About CSS Margin And Padding
Styling spaces created by either padding or margin are mainly invisible. But when the background color is added to the mix, padding often...
Read more >
API - Styled System
The space utility converts shorthand margin and padding props to margin and padding CSS declarations. Numbers from 0 to the length of theme.space...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found