UNPKG

@wordpress/block-library

Version:
1 lines • 9.89 kB
{"version":3,"file":"responsive-styles.cjs","names":["MAX_COLUMNS"],"sources":["../../src/gallery/responsive-styles.js"],"sourcesContent":["import { MAX_COLUMNS } from './constants';\n\nfunction isObject( value ) {\n\treturn !! value && typeof value === 'object' && ! Array.isArray( value );\n}\n\nexport function isValidGalleryColumns( value ) {\n\treturn Number.isInteger( value ) && value >= 1 && value <= MAX_COLUMNS;\n}\n\n/*\n * Aspect ratios are interpolated into a generated stylesheet instead of being\n * set as an inline style, so only the numeric forms produced by aspect ratio\n * presets (and `auto`) are accepted. A value from saved content that isn't one\n * of those is ignored rather than emitted, so it can't close the rule early and\n * inject declarations of its own. The frontend applies the same restriction in\n * `block_core_gallery_is_valid_aspect_ratio()`.\n */\nconst ASPECT_RATIO_PATTERN = /^(auto|\\d+(\\.\\d+)?(\\s*\\/\\s*\\d+(\\.\\d+)?)?)$/;\n\n/**\n * Returns whether a value can be used as a Gallery aspect ratio.\n *\n * @param {*} value Value to check.\n * @return {boolean} Whether the value is a valid aspect ratio.\n */\nexport function isValidGalleryAspectRatio( value ) {\n\treturn (\n\t\ttypeof value === 'string' && ASPECT_RATIO_PATTERN.test( value.trim() )\n\t);\n}\n\n/**\n * Returns Gallery styles for a viewport.\n *\n * @param {Object} style Gallery block style attribute.\n * @param {string} viewport Viewport state.\n * @return {Object} Gallery viewport styles, or an empty object when unavailable.\n */\nexport function getViewportGalleryStyle( style, viewport ) {\n\tif ( ! isObject( style ) || ! isObject( style[ viewport ] ) ) {\n\t\treturn {};\n\t}\n\n\treturn style[ viewport ];\n}\n\nfunction cleanObject( value ) {\n\tif ( ! isObject( value ) ) {\n\t\treturn value;\n\t}\n\n\tconst entries = Object.entries( value ).filter(\n\t\t( [ , entryValue ] ) => entryValue !== undefined\n\t);\n\treturn entries.length ? Object.fromEntries( entries ) : undefined;\n}\n\n/**\n * Updates Gallery layout settings for a viewport without changing base values.\n *\n * @param {Object} options Options.\n * @param {Object} options.style Gallery block style attribute.\n * @param {string} options.viewport Selected viewport state.\n * @param {Object} options.baseSettings Gallery base layout settings.\n * @param {Object} options.settings Gallery layout settings to update.\n * @return {Object|undefined} Updated Gallery style attribute.\n */\nexport function getUpdatedGalleryStyle( {\n\tstyle,\n\tviewport,\n\tbaseSettings,\n\tsettings,\n} ) {\n\tconst currentStyle = isObject( style ) ? style : {};\n\tconst nextViewportStyle = {\n\t\t...getViewportGalleryStyle( currentStyle, viewport ),\n\t};\n\n\tObject.entries( settings ).forEach( ( [ key, value ] ) => {\n\t\t/* Columns is an exception to deletion of duplicate styles because\n\t\t * we need to be able to override the legacy responsive styles that\n\t\t * default to 2 columns under 600px.\n\t\t */\n\t\tif (\n\t\t\tvalue === undefined ||\n\t\t\t( key !== 'columns' && value === baseSettings[ key ] )\n\t\t) {\n\t\t\tdelete nextViewportStyle[ key ];\n\t\t} else {\n\t\t\tnextViewportStyle[ key ] = value;\n\t\t}\n\t} );\n\n\tconst cleanedViewportStyle = cleanObject( nextViewportStyle );\n\tconst nextStyle = { ...currentStyle };\n\tif ( cleanedViewportStyle ) {\n\t\tnextStyle[ viewport ] = cleanedViewportStyle;\n\t} else {\n\t\tdelete nextStyle[ viewport ];\n\t}\n\n\treturn cleanObject( nextStyle );\n}\n\nfunction getGallerySelector( selector ) {\n\treturn `${ selector }.wp-block-gallery.has-nested-images`;\n}\n\nfunction getFlexGallerySelector( selector ) {\n\treturn `${ getGallerySelector( selector ) }:where(.is-layout-flex)`;\n}\n\nfunction getImageSelector( selector ) {\n\treturn `${ getFlexGallerySelector(\n\t\tselector\n\t) } figure.wp-block-image:not(#individual-image)`;\n}\n\nfunction getColumnsCSS( selector, columns ) {\n\t// Match the fallback used by the Gallery's existing column-width CSS and\n\t// server-rendered responsive styles when the gap custom property is unavailable.\n\tconst width =\n\t\tcolumns === 1\n\t\t\t? '100%'\n\t\t\t: `calc((100% - (var(--wp--style--unstable-gallery-gap, 16px) * ${\n\t\t\t\t\tcolumns - 1\n\t\t\t\t})) / ${ columns })`;\n\n\treturn `${ getImageSelector( selector ) }{width:${ width } !important;}`;\n}\n\nfunction getImageCropCSS( selector, imageCrop ) {\n\tconst imageSelector = getImageSelector( selector );\n\tconst wrapperSelector = `${ imageSelector } > div:not(.components-drop-zone)`;\n\tconst linkSelector = `${ imageSelector } > a`;\n\tconst mediaSelector = `${ imageSelector } a,${ imageSelector } img`;\n\n\tif ( imageCrop ) {\n\t\treturn `${ imageSelector }{align-self:inherit !important;margin-bottom:0 !important;}${ wrapperSelector },${ linkSelector }{display:flex !important;}${ mediaSelector }{width:100% !important;flex:1 0 0% !important;height:100% !important;object-fit:cover !important;}`;\n\t}\n\n\treturn `${ imageSelector }{align-self:auto !important;margin-top:0 !important;margin-bottom:auto !important;}${ wrapperSelector }{display:block !important;}${ linkSelector }{display:inline-block !important;}${ mediaSelector }{width:auto !important;flex:0 1 auto !important;height:auto !important;object-fit:fill !important;}`;\n}\n\nfunction getAspectRatioCSS( selector, aspectRatio ) {\n\tconst imageSelector = `${ getGallerySelector(\n\t\tselector\n\t) } figure.wp-block-image:not(#individual-image) img`;\n\tconst ratio = aspectRatio.trim();\n\n\tif ( ratio === 'auto' ) {\n\t\t/*\n\t\t * We need an explicit value here to cancel out the base aspect ratio. `auto` — and\n\t\t * `initial`, `unset` and `revert`, which all compute to it — would\n\t\t * override the `width`/`height` presentational hint that gives a\n\t\t * lazy-loaded image its placeholder ratio, collapsing the image to zero\n\t\t * height until it loads.\n\t\t * `revert-layer` drops the declaration instead, so the image falls back\n\t\t * to that hint while loading, to its natural ratio once loaded, and to\n\t\t * any ratio a theme set in a lower cascade layer. `object-fit` is left\n\t\t * as the base set it, so a cropped Gallery still crops.\n\t\t */\n\t\treturn `${ imageSelector }{aspect-ratio:revert-layer !important;}`;\n\t}\n\n\treturn `${ imageSelector }{aspect-ratio:${ ratio } !important;object-fit:cover !important;}`;\n}\n\n/**\n * Generates Gallery-specific responsive CSS.\n *\n * @param {string} selector Gallery block selector.\n * @param {Object} style Gallery block style attribute.\n * @param {Object} mediaQueries Map of viewport keys to media queries.\n * @return {string} Responsive Gallery CSS.\n */\nexport function getGalleryResponsiveCSS( selector, style, mediaQueries ) {\n\tif ( ! isObject( mediaQueries ) ) {\n\t\treturn '';\n\t}\n\n\treturn Object.entries( mediaQueries )\n\t\t.map( ( [ viewport, mediaQuery ] ) => {\n\t\t\tif ( typeof mediaQuery !== 'string' ) {\n\t\t\t\treturn '';\n\t\t\t}\n\n\t\t\tconst viewportStyle = getViewportGalleryStyle( style, viewport );\n\t\t\tlet css = '';\n\n\t\t\tif ( isValidGalleryColumns( viewportStyle.columns ) ) {\n\t\t\t\tcss += getColumnsCSS( selector, viewportStyle.columns );\n\t\t\t}\n\n\t\t\tif ( typeof viewportStyle.imageCrop === 'boolean' ) {\n\t\t\t\tcss += getImageCropCSS( selector, viewportStyle.imageCrop );\n\t\t\t}\n\n\t\t\tif ( isValidGalleryAspectRatio( viewportStyle.aspectRatio ) ) {\n\t\t\t\tcss += getAspectRatioCSS( selector, viewportStyle.aspectRatio );\n\t\t\t}\n\n\t\t\treturn css ? `${ mediaQuery }{${ css }}` : '';\n\t\t} )\n\t\t.join( '' );\n}\n"],"mappings":";;;;;;;AAEA,SAAS,SAAU,OAAQ;CAC1B,OAAO,CAAC,CAAE,SAAS,OAAO,UAAU,YAAY,CAAE,MAAM,QAAS,KAAM;AACxE;AAEA,SAAgB,sBAAuB,OAAQ;CAC9C,OAAO,OAAO,UAAW,KAAM,KAAK,SAAS,KAAK,SAASA,eAAAA;AAC5D;AAUA,MAAM,uBAAuB;;;;;;;AAQ7B,SAAgB,0BAA2B,OAAQ;CAClD,OACC,OAAO,UAAU,YAAY,qBAAqB,KAAM,MAAM,KAAK,CAAE;AAEvE;;;;;;;;AASA,SAAgB,wBAAyB,OAAO,UAAW;CAC1D,IAAK,CAAE,SAAU,KAAM,KAAK,CAAE,SAAU,MAAO,SAAW,GACzD,OAAO,CAAC;CAGT,OAAO,MAAO;AACf;AAEA,S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