I'm working on a custom block that renders a series of DIVs differently depending on the device on which it is displayed. On desktop, the DIVs need to be displayed horizontally with a right margin of x pixels, where x can be dynamically altered via a block inspector control. On mobile, the DIVs need to be displayed vertically with a bottom margin of x pixels, where x is controlled by the same setting.
One can use the style attribute to add inline dynamic styles to a block in the edit
function, like this:
<div className="item" style={(window.innerWidth < 768) ? { marginBottom: gap } : { marginRight: gap }} key={index}>
where gap
is a dynamic value set via the block inspector.
But this cannot be used in a dynamic block rendered via PHP in the frontend.
The ideal solution would be to embed an inline CSS media query for this (a static one cannot be used because of the gap
value), but I don't know how to attach one to a block on both the editor and the render_callback
function for the frontend.
Any help will be greatly appreciated.