How to pass padding/margin as props in React-Bootstrap components

前端 未结 6 1530
闹比i
闹比i 2021-02-15 11:09

I am trying to apply margins and paddings with React-Bootstrap as props.

I passed the docs through but haven\'t found any mention adding padding or margin in there as it

6条回答
  •  情话喂你
    2021-02-15 11:55

    None of the jQuery-free implementations of Bootstrap (React Bootstrap, BootstrapVue or ngBootstrap) have implemented utility directives for spacing (margin/padding), simply because Bootstrap have made it unnecessary in the vast majority of cases, by providing a very intuitive set of Spacing utility classes.

    All you need to do is apply the desired class.


    To apply utility classes selectively, based on responsiveness interval (media queries), you could use a useMedia hook, as demoed here.
    In a nutshell:

    const interval = useMedia([
        "(min-width: 1200px)",
        "(min-width: 992px)",
        "(min-width: 768px)",
        "(min-width: 576px)"
      ],
      ["xl", "lg", "md", "sm"],
      "xs"
    );
    

    (Based on useMedia from useHooks/useMedia).

    You can now reuse this hook throughout your app to add media interval based logic.

    Example usages:

    // interval === 'sm' ? a : b
    // ['xs', 'sm'].includes(interval) ? a : b
    // negations of the above, etc...
    

    Important: this particular implementation returns the first matching media query in the list.

    If you need to map various media queries, to an object/map with true/false values, you'll need to modify getValue fn to return the entire list, along these lines:

    const getValue = () => {
      const matches =  mediaQueryLists.map(mql => mql.matches);
      return values.reduce((o, k, i) => ({...o, [k]: matches[i]}), {})
    };
    

    Working example here.
    Obviously, you could expand on it and add/remove queries. However, be warned each query adds a separate listener so it could impact performance.

    In most cases, the return of the first matching query (first example) is enough.

    Note: if the above useMedia hook is not enough for your use case, a more robust and heavily tested solution for media-query listeners in JS is enquire.js. It's easy to use, incredibly light and thoroughly tested cross-browser/cross-device. I have no affiliation with it, but I have used it in various projects over the course of more than a decade. In short, I couldn't recommend it more.


    Back to Bootstrap 4: in order to customize the $spacer sizes, follow the guide provided under Bootstrap's theming as it's actually about more than what we typically call theming (changing colors), it's about overriding default values of Bootstrap's SASS defaults, including responsivenss breakpoints, spacers, number of columns and many, many others. The one you're interested in is $spacer.
    Simply write the overrides into an .scss file and import it in your root component. Example.

    Note: a (simpler and more intuitive) option to customize Bootstrap is to do it visually, using bootstrap.build but it's typically a few minor versions behind (i.e. Bootstrap is now at v4.4.1 and the build tool is at v4.3.0).
    The build customizer provides intuitive controls and real time visualization.
    It allows export as .css or .scss.

提交回复
热议问题