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.

[Button+] Multiple props are not reponsive

See original GitHub issue

Describe the bug A couple of props do not work when you give them responsive values these include the variantColor and size props.

Expected Behavior When the size prop receives a value of ['md','lg'] it should use the corresponding value at the corresponding breakpoint

To Reproduce check out this sandbox: https://codesandbox.io/s/unresponsive-props-chakra-q4xyf

  1. Look at the source code
  2. The button is not displayed at all (both variantColor and size ar ignored as they are arrays), same applies to the ‘direction’ of the stack

Suggested solution(s)

This bug happens because chakra modifies or accesses these values with a function. I use a helper in my projects that makes it possible to also modify responsive arrays and objects and fixes that issue.

function modifyResponsiveValue(value, cb) {
  if (typeof value === 'string' || typeof value === 'number') {
    return cb(value)
  } else if (Array.isArray(value)) {
    return value.map(cb)
  } else {
    const newObj = {}
    Object.keys(value).forEach(k => (newObj[k] = cb(value[k])))
    return newObj
  }
}

Desktop (please complete the following information):

  • OS: linux
  • Browser chrome

Also happy to make a pr for this after the ts rewrite

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:8
  • Comments:7

github_iconTop GitHub Comments

12reactions
adamjarlingcommented, Jan 19, 2021

Here’s one way to currently handle responsive button size, using Chakra UI’s useBreakpointValue() hook:

import {
  Button,
  useBreakpointValue,
} from '@chakra-ui/react';

function MyComponent() {
  const buttonSize = useBreakpointValue({ base: 'xs', sm: 'sm', md: 'md' });

  return (
    <Button size={buttonSize}>Responsive Button</Button>
  );
}
2reactions
Timonzimmcommented, Dec 29, 2021

Still an issue to this date.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I pass multiple props in a single event handler in ...
I'm trying to add a onClick listener to a li and the props will be passed to another component that ...
Read more >
How passing props to component works in React
Master how to pass props (properties) to components in React with this useful beginner's guide, including demo code.
Read more >
How to pass multiple props in a single event handler in ...
Method 1: We can make a separate method for the event and call all the props method in that method.
Read more >
4 Patterns for Responsive Props in React - DEV Community ‍ ‍
Let's say you have different button sizes: large, medium, small. You probably don't want all buttons to change size responsively the same way....
Read more >
How to use Props in React - Robin Wieruch
React props are not being passed in Components; React props key is ... it can be confusing for React beginners, because you have...
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