Utilities API
import { <utilityName> } from "muuri-react";
withHooks ๐ฆ
The withHooks utility allows you to use hooks with class
components. See more here
Arguments
- component ย โย React.ReactElement
- The dragged Item.
- enabledHooks ย โย string[]
- The hooks to enable.
const WrappedItem = withHooks(Item, ['useDrag']);
For each hook this utility generate specific props
and pass them to the Item
. The following are the nomenclature conventions, to see in depth the APIs of an hook you can check their documentation pages.
Hook | Props |
---|---|
useData | { setData } |
useDrag | { isDragging } |
useDraggable | { setDraggable } |
useGrid | { gridData } |
useRefresh | { refresh } |
useShow | { isShowing } |
useVisibility | { setVisibility } |
getResponsiveStyle ๐ฆ
This utility allows you to generate the style of the Item
and make it responsive. See more here.
Arguments
- options.columns ย โย number
- A fraction that has in the denominator the number of columns in which you want to virtually divide the MuuriComponent, and in the numerator the number of columns that the Item must occupy.
- Required.
- options.margin ย โย string / number
- Default value:
"0px"
. - The margin of the Item. It can be any valid value for the CSS margin property, expressed in
px
or%
.
- Default value:
- options.ratio ย โย number
- The width / height ratio of the Item. If you need a static height use the
height
option instead.
- The width / height ratio of the Item. If you need a static height use the
- options.height ย โย string / number
- The static height, expressed in px, of the Item. If you need a responsive height use the
ratio
option instead.
- The static height, expressed in px, of the Item. If you need a responsive height use the
// The responsive style.
const responsiveStyle = getResponsiveStyle({
columns: 3 / 8,
margin: '5% 2%',
ratio: 1.5,
});
// The Item component.
const Item = () => (
<div style={responsiveStyle}>
<div className="item-content" />
</div>
);
getStaticStyle ๐ฆ
This utility allows you to generate the style of the Item
and make it static. See more here.
Arguments
- options.grid ย โย Muuri
- A fraction that has in the denominator the number of columns in which you want to virtually divide the MuuriComponent, and in the numerator the number of columns that the Item must occupy.
- Required.
- options.columns ย โย number
- A fraction that has in the denominator the number of columns in which you want to virtually divide the MuuriComponent, and in the numerator the number of columns that the Item must occupy.
- Required.
- options.margin ย โย string / number
- Default value:
"0px"
. - The margin of the Item. It can be any valid value for the CSS margin property, expressed in
px
or%
.
- Default value:
- options.ratio ย โย number
- The width / height ratio of the Item. If you need a static height use the
height
option instead.
- The width / height ratio of the Item. If you need a static height use the
- options.height ย โย string / number
- The static height, expressed in px, of the Item. If you need a responsive height use the
ratio
option instead.
- The static height, expressed in px, of the Item. If you need a responsive height use the
// The Item component.
const Item = () => {
// Get the Muuri instance.
const {grid} = useGrid();
// If the item is dragging.
const isDragging = useDrag();
// Item dimensions.
const columns = 3 / 8;
const margin = '5%';
const ratio = 1.5;
// If the item is dragging the style
// has to be static.
const style = !isDragging
? getResponsiveStyle({columns, margin, ratio})
: getStaticStyle({grid, columns, margin, ratio});
return (
<div style={style}>
<div className="item-content" />
</div>
);
};