@wordpress/block-library
Version:
Block library for the WordPress editor.
57 lines (56 loc) • 3.1 kB
JavaScript
import { __experimentalGetGapCSSValue, privateApis, store, useSettings, useStyleOverride } from "@wordpress/block-editor";
import { useSelect } from "@wordpress/data";
import { privateApis as privateApis$1 } from "@wordpress/global-styles-engine";
import { unlock } from "../lock-unlock.mjs";
import { getGalleryResponsiveCSS } from "./responsive-styles.mjs";
// packages/block-library/src/gallery/gallery-styles.js
const { getResponsiveMediaQueries } = unlock(privateApis$1);
const { globalStylesDataKey } = unlock(privateApis);
const GALLERY_BLOCK_NAME = "core/gallery";
const FALLBACK_VALUE = `var( --wp--style--gallery-gap-default, var( --gallery-block--gutter-size, var( --wp--style--block-gap, 0.5em ) ) )`;
function getGalleryGapCustomPropertyStyle(selector, blockGap) {
let column = FALLBACK_VALUE;
if (blockGap) column = typeof blockGap === "string" ? __experimentalGetGapCSSValue(blockGap) || FALLBACK_VALUE : __experimentalGetGapCSSValue(blockGap?.left) || FALLBACK_VALUE;
return `${selector} {
--wp--style--unstable-gallery-gap: ${column === "0" ? "0px" : column}
}`;
}
function getBlockGapValue(style) {
if (!Object.hasOwn(style?.spacing || {}, "blockGap")) return;
return style.spacing.blockGap;
}
/**
* Renders the Gallery's instance styles: the gap custom property the Flex
* layout's column widths are calculated from, and the Gallery's responsive
* viewport CSS.
*
* @param {Object} props
* @param {Object} props.style Gallery block style attribute.
* @param {string} props.clientId Gallery block client ID.
* @param {boolean} props.isFlexLayout Whether the Gallery uses the Flex layout.
*/
function GalleryStyles({ style, clientId, isFlexLayout }) {
const selector = `.wp-block-gallery-${clientId}`;
const [viewportSettings] = useSettings("viewport");
const globalStyles = useSelect((select) => select(store).getSettings()?.[globalStylesDataKey], []);
const responsiveMediaQueries = getResponsiveMediaQueries(viewportSettings);
let css = "";
if (isFlexLayout) {
const globalGalleryStyles = globalStyles?.blocks?.[GALLERY_BLOCK_NAME] || {};
const styleBlockGap = getBlockGapValue(style);
const globalGalleryBlockGap = globalGalleryStyles?.spacing?.blockGap ?? FALLBACK_VALUE;
css += getGalleryGapCustomPropertyStyle(selector, styleBlockGap === void 0 ? globalGalleryBlockGap : styleBlockGap);
Object.entries(responsiveMediaQueries).forEach(([viewport, mediaQuery]) => {
const styleViewportBlockGap = getBlockGapValue(style?.[viewport]);
const globalViewportBlockGap = styleBlockGap === void 0 ? globalGalleryStyles?.[viewport]?.spacing?.blockGap : void 0;
const viewportBlockGap = styleViewportBlockGap === void 0 ? globalViewportBlockGap : styleViewportBlockGap;
if (viewportBlockGap === void 0 || viewportBlockGap === null) return;
css += `${mediaQuery}{${getGalleryGapCustomPropertyStyle(selector, viewportBlockGap)}}`;
});
}
css += getGalleryResponsiveCSS(selector, style, responsiveMediaQueries);
useStyleOverride({ css });
return null;
}
export { GalleryStyles as default };
//# sourceMappingURL=gallery-styles.mjs.map