NativeBase icon indicating copy to clipboard operation
NativeBase copied to clipboard

Cannot remove button padding in variants.

Open elenitaex5 opened this issue 3 years ago • 9 comments

Description

Working with button variants, props like p, px and py doesn't work

CodeSandbox/Snack link

https://codesandbox.io/s/ecstatic-frog-uld3wb

Steps to reproduce

  1. Create theme variant for button.
  2. Create a button in your app.
  3. You can padding even there's padding props as 0

NativeBase Version

3.4.15

Platform

  • [X] Android
  • [ ] CRA
  • [X] Expo
  • [X] iOS
  • [ ] Next

Other Platform

Storybook

Additional Information

Hi! I'm working with Native Base Button variants, creating new Link variant, but padding modify only works if it pass through component like this.

<Button isDisabled={disabled} variant={buttonType} onPress={onPress} px={0} py={0}>
    {!loading ? text : <BeatingHeartIcon />}
</Button>
<Button text={t('login.request_a_new_one')} buttonType={'link'} onPress={() => {}} />
const variantLink = (props: Dict) => {
  const c = props?.theme?.colors

  return {
    bg: 'none',
    width: 'auto',
    px: 0,
    py: 0,
    _text: {
      color: c?.green?.main,
      fontWeight: 700
    }
  }
}

export const buttonVariants = {
  primary: variantPrimary,
  link: variantLink
}

But if I remove px and py and include it in variant as codesandbox below, padding still appears (like screenshot)

image

elenitaex5 avatar Sep 16 '22 12:09 elenitaex5

Hey @elenitaex5, It may be possible that you have not passed that into variants inside button theme.

Can you try out this below snippet? Let me know if it works for you.

const theme = extendTheme({
  components: {
    Button: {
      variants: buttonVariants
    }
  }
})

ankit-tailor avatar Sep 22 '22 07:09 ankit-tailor

Hey @elenitaex5, It may be possible that you have not passed that into variants inside button theme.

Can you try out this below snippet? Let me know if it works for you.

const theme = extendTheme({
  components: {
    Button: {
      variants: buttonVariants
    }
  }
})

Hi @ankit-tailor it isn't the problem. Button variants are working, and they are inside extendTheme problem only happens trying to modify padding in variants. Everything else is working right.

elenitaex5 avatar Sep 22 '22 07:09 elenitaex5

@elenitaex5 I had this issue as well trying to mess with font sizes. There is a default prop of size: 'md' on the button which has default sizes and paddings. Variants don't seem to overwrite these styles, try this and let me know if that helps.

const theme = extendTheme({
  components: {
    Button: {
      variants: buttonVariants
      sizes: {
        py: null,
        px: null
      },
      // or even better
      defaultProps: {
        size: null
      }
    }
  }
})

mikehuebner avatar Sep 22 '22 16:09 mikehuebner

Hi @mikehuebner thanks for your help!!! It works for me. Hope this bug be solved in a future.

elenitaex5 avatar Sep 27 '22 07:09 elenitaex5

Hi Everyone, So here, Variant will be resolved first and then sizes will be resolved. So fontSize from size will override fontSize from variant. This might help you.

ankit-tailor avatar Sep 30 '22 06:09 ankit-tailor

@ankit-tailor problems are overriding paddings, do you mean creating custom sizes in button?

This is really an issue, if you're specifying padding, it must override default paddings.

elenitaex5 avatar Sep 30 '22 07:09 elenitaex5

Because (as @ankit-tailor mentioned) sizes is resolved after variant, I have been able to get it to work by passing a function to the sizes.lg object and use the variant value to set the size:

const theme = extendTheme({
  components: {
    Button: {
      defaultProps: {
        size: 'lg',
        variant: 'solid',
      },
      sizes: {
        lg: ({ variant }) => {
          const pMap = { solid: 4, link: 0 };
          return {
            py: pMap[variant],
            px: 3,
            _text: {
              fontSize: 'md',
            },
            _icon: {
              size: 'md',
            },
          };
        },
      },
      variants: buttonVariants,
    },
  },
});

A side-effect of this approach is that the returned object no longer "extends" the default lg object. So in my example, I've added the remaining "default" values for px, _text, and _icon. Omitting these values will still work, but you may experience some unexpected issues.

coreyroach avatar Dec 14 '22 00:12 coreyroach

This working for me.

Button: {
   variants: {
      link: {
          _important: { p: "0", h: "auto" },
      },
   },
},

krausediego avatar Aug 29 '23 16:08 krausediego

@krausediego there are many ways of fixing that, but without _important props should be overwritten as well. Maybe in new gluestack it would be fixed, but I think no one of this issues are gonna be fixed.

elenitaex5 avatar Aug 30 '23 07:08 elenitaex5