refactor(dashboard-v2): use breakpoint names instead of minWidth in Slider props

This commit is contained in:
Michał Leszczyk 2022-02-26 12:48:24 +01:00
parent 7681287127
commit 278ab698c2
No known key found for this signature in database
GPG Key ID: FA123CA8BAA2FBF4
2 changed files with 32 additions and 6 deletions

View File

@ -86,8 +86,20 @@ Slider.propTypes = {
slides: PropTypes.arrayOf(PropTypes.node.isRequired), slides: PropTypes.arrayOf(PropTypes.node.isRequired),
breakpoints: PropTypes.arrayOf( breakpoints: PropTypes.arrayOf(
PropTypes.shape({ PropTypes.shape({
minWidth: PropTypes.number.isRequired, /**
* Breakpoint name as defined in Tailwind config. If not defined, config
* will be applied to all non-configured screen sizes.
*/
name: PropTypes.string,
/**
* Number of slides visible for a given breakpoint.
*/
visibleSlides: PropTypes.number.isRequired, visibleSlides: PropTypes.number.isRequired,
/**
* Whether or not the list should be scrollable horizontally at the given breakpoint.
* If set to false, all slides will be visible & rendered in a column.
*/
scrollable: PropTypes.bool.isRequired,
}) })
), ),
}; };
@ -95,18 +107,17 @@ Slider.propTypes = {
Slider.defaultProps = { Slider.defaultProps = {
breakpoints: [ breakpoints: [
{ {
minWidth: parseInt(theme.screens.xl), name: "xl",
scrollable: true, scrollable: true,
visibleSlides: 3, visibleSlides: 3,
}, },
{ {
minWidth: parseInt(theme.screens.md, 10), name: "md",
scrollable: true, scrollable: true,
visibleSlides: 2, visibleSlides: 2,
}, },
{ {
// For the smallest screens, we won't scroll but instead stack the slides vertically. // For the smallest screens, we won't scroll but instead stack the slides vertically.
minWidth: -Infinity,
scrollable: false, scrollable: false,
visibleSlides: 1, visibleSlides: 1,
}, },

View File

@ -1,11 +1,26 @@
import { useEffect, useMemo, useCallback, useState } from "react"; import { useEffect, useMemo, useCallback, useState } from "react";
import { useWindowSize } from "react-use"; import { useWindowSize } from "react-use";
import theme from "../../lib/theme";
const { screens } = theme;
export default function useActiveBreakpoint(breakpoints) { export default function useActiveBreakpoint(breakpoints) {
const { width: windowWidth } = useWindowSize(); const { width: windowWidth } = useWindowSize();
// Since our breakpoints are setup with min-width rule, we need to sort them from largest to smallest
const monitoredBreakpoints = useMemo( const monitoredBreakpoints = useMemo(
() => breakpoints.slice().sort(({ minWidth: widthA }, { minWidth: widthB }) => widthB - widthA), () =>
breakpoints
.slice()
// Map breakpoint names to their min-width configured in Tailwind
.map(({ name, ...config }) => {
// If breakpoint name is not configured,
// we'll apply this config to all unmatched breakpoints.
const minWidth = screens[name] ? parseInt(screens[name], 10) : -Infinity;
return { minWidth, ...config };
})
// Since our breakpoints are setup with min-width rule, we need to sort them from largest to smallest
.sort(({ minWidth: widthA }, { minWidth: widthB }) => widthB - widthA),
[breakpoints] [breakpoints]
); );
const findActiveBreakpoint = useCallback( const findActiveBreakpoint = useCallback(