Last active
May 2, 2021 15:05
-
-
Save gaurangrshah/f34ceecebac0a580af2c4281345fc001 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const colorPalette = theme.colors[color] | |
export function borders(children, color = "red") { | |
let colors = Object.keys(theme.colors[color]); | |
colors = shuffle(colors); | |
return React.Children.map(children, (child, i) => { | |
const borderProps = { | |
style: { border: `1px solid ${colorPalette[colors[i]]}` }, | |
}; | |
const props = Object.assign({}, child.props, borderProps); | |
return <child.type key={i} {...props} />; | |
}); | |
} | |
// NOTE: provider an array of colors to color palette, current implementation works with any system-ui framework. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function combineProviders(providers) { | |
return providers.reduce((Combined, Provider) => ({ children }) => ( | |
<Combined> | |
<Provider>{children}</Provider> | |
</Combined> | |
)); | |
/*** | |
* USAGE: | |
const Providers2 = combineProviders(providers); | |
const App = () => { | |
return ( | |
<Providers> | |
{children} | |
</Providers> | |
) | |
} | |
*/ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @link: https://itnext.io/improving-slow-mounts-in-react-apps-cff5117696dc | |
const Defer = ({ chunkSize, children }) => { | |
const [renderedItemsCount, setRenderedItemsCount] = React.useState(chunkSize); | |
const childrenArray = React.useMemo(() => React.Children.toArray(children), [ | |
children | |
]); | |
React.useEffect(() => { | |
if (renderedItemsCount < childrenArray.length) { | |
window.requestIdleCallback( | |
() => { | |
setRenderedItemsCount( | |
Math.min(renderedItemsCount + chunkSize, childrenArray.length) | |
); | |
}, | |
{ timeout: 200 } | |
); | |
} | |
}, [renderedItemsCount, childrenArray.length, chunkSize]); | |
return childrenArray.slice(0, renderedItemsCount); | |
}; | |
/* | |
USAGE: | |
<Defer chunkSize={5}> | |
{items.map(item => <Card key={item.id} item={item} />)} | |
</Defer> | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function propper(children, childProps) { | |
// @SCOPE: used to assign the same set of props to every child | |
return React.Children.map(children, (child, i) => { | |
const props = Object.assign({}, child.props, childProps); | |
return <child.type key={i} {...props} />; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment