react-flexbox-grid icon indicating copy to clipboard operation
react-flexbox-grid copied to clipboard

xs={0} or md={0} for hide

Open ivansglazunov opened this issue 9 years ago • 13 comments
trafficstars

Why not make it easy to hide elements at certain screen sizes? Please please please :)

ivansglazunov avatar Aug 26 '16 14:08 ivansglazunov

Not sure about the props. In the past I've created <Visible> or <Hidden> elements. This might be a better approach? Not sure if that really fits in with the scope of this project though, but I certainly appreciate the need for it.

E.g.

<Visible xs sm>
    <h1>Only on mobile!</h1>
</Visible>

oliverbenns avatar Nov 15 '16 14:11 oliverbenns

👍 This would be very useful

arjshiv avatar Mar 06 '17 15:03 arjshiv

@arjshiv follow #82 for news about this. The feature has not yet landed it flexboxgrid itself, as I understood.

silvenon avatar Mar 06 '17 15:03 silvenon

@silvenon Thanks!

In the meantime, I'm using a solution similar to @oliverbenns, but with a pure CSS show-on-mobile / hide-on-mobile class that toggles display using media queries. As much as I'd like a pure React solution, this helps me solve this quickly for mobile screens without extra React components to handle display.

arjshiv avatar Mar 06 '17 16:03 arjshiv

@ivansglazunov i really liked your proposal so I wanted to share this snippet which wraps the original component adding support for something like: <Col small={0} large={12} />

I simplified the API to small and large distinctions because I only care about distinguishing phones from tablets and desktops, but feel free to modify it for your needs

Usage:

<Col small={0} large={12} /> /* displays only on medium/large devices (tablets, desktops, large monitors) */
<Col small={12} large={0} /> /* displays only on phones/small devices (phones) */
import React, { PureComponent, PropTypes } from 'react'

import {
  Grid,
  Row,
  Col as FlexCol,
} from 'react-flexbox-grid'

import cn from 'classnames'

class Col extends PureComponent {
  // NOTE: add /fleboxgrid/ to css loader webpack config before use
  // https://github.com/roylee0704/react-flexbox-grid#minimal-configuration

  static propTypes: {
    className: PropTypes.string,
    // less than 768px (phones)
    xs: PropTypes.number,
    // min width of 768px (tablets)
    sm: PropTypes.number,
    // min width of 1024px (laptops/desktops)
    md: PropTypes.number,
    // min width of 1200px (large monitors)
    lg: PropTypes.number,
    // less than 768px (phones)
    small: PropTypes.number,
    // more than 768px (anything larger than a phone)
    large: PropTypes.number,
  }

  get mappedProps() {
    const clonedProps = Object.assign({}, this.props)
    const newProps = {}

    const { small, large } = this.props

    if (typeof small === 'number') {
      if (small <= 0) {
        newProps.className = cn(this.props.className, 'hideOnSmall')
      } else {
        newProps.xs = small
      }
    }

    if (typeof large === 'number') {
      if (large <= 0) {
        newProps.className = cn(this.props.className, 'hideOnLarge')
      }
      newProps.sm = large
      newProps.md = large
      newProps.lg = large
    }

    delete clonedProps.small
    delete clonedProps.large

    return {
      ...clonedProps,
      ...newProps,
    }
  }

  render() {
    return (
      <FlexCol {...this.mappedProps} />
    )
  }
}

export { Row, Col }
export default Grid

From here you can easily toggle display via class toggles and media queries:

/* global.css */
@media screen and (max-width: 768px) {
  :global .hideOnMobile {
    display: none;
  }
}

@media screen and (min-width: 768px) {
  :global .hideOnLarge {
    display: none;
  }
}

x

lfender6445 avatar Jul 10 '17 14:07 lfender6445

+1

Please merge!

tawanorg avatar Sep 29 '17 10:09 tawanorg

This has been a long time coming? Seems like flexbox css has been abandoned – does that mean this project is going to become stale too?

benoj avatar Oct 09 '17 19:10 benoj

Should this issue be marked as closed in favour of https://github.com/roylee0704/react-flexbox-grid/pull/138?

Just use className with any of these in your Column or Row: .hidden-xs .hidden-sm .hidden-md .hidden-lg .hidden-xl

Fore more info: https://github.com/evgenyrodionov/flexboxgrid2/pull/4

anton6 avatar Apr 07 '18 23:04 anton6

Any update on this? Is this project still maintained?

isaachinman avatar Oct 08 '18 18:10 isaachinman

Would still be useful

marcramser avatar May 28 '19 06:05 marcramser

Very useful

jpcmf avatar May 28 '20 20:05 jpcmf

Very useful

patrickml avatar Feb 24 '21 13:02 patrickml

if you guys are not using boostrap or other there is one solution using react useQueryMedia hook. i would be glad if we can handle those thing by grid itself like fxlayout.

jigmeloday avatar Mar 21 '22 12:03 jigmeloday