@empathyco/x-components 10.3.0 → 10.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/docs/API-reference/api/x-components.arraytoobject.md +1 -1
- package/docs/API-reference/api/x-components.baseresultimage.md +39 -5
- package/docs/API-reference/api/x-components.createusedevice.md +1 -1
- package/docs/API-reference/api/x-components.deepmergewithdefaults.md +1 -1
- package/docs/API-reference/api/x-components.md +3 -1
- package/docs/API-reference/api/x-components.searchconfig.md +19 -0
- package/docs/API-reference/api/x-components.searchconfig.pricesortfield.md +13 -0
- package/docs/API-reference/api/x-components.xinstaller.md +4 -4
- package/docs/API-reference/api/x-components.xplugin.md +1 -1
- package/docs/API-reference/components/common/result/x-components.base-result-image.md +161 -6
- package/js/components/base-dropdown.vue.js +2 -6
- package/js/components/base-dropdown.vue.js.map +1 -1
- package/js/components/base-grid.vue.js +2 -8
- package/js/components/base-grid.vue.js.map +1 -1
- package/js/components/column-picker/base-column-picker-list.vue.js +1 -1
- package/js/components/column-picker/base-column-picker-list.vue.js.map +1 -1
- package/js/components/result/base-result-image.vue.js +18 -15
- package/js/components/result/base-result-image.vue.js.map +1 -1
- package/js/components/result/base-result-image.vue2.js +125 -9
- package/js/components/result/base-result-image.vue2.js.map +1 -1
- package/js/x-modules/facets/components/facets/facets.vue.js +4 -13
- package/js/x-modules/facets/components/facets/facets.vue.js.map +1 -1
- package/js/x-modules/facets/components/lists/selected-filters-list.vue.js +3 -9
- package/js/x-modules/facets/components/lists/selected-filters-list.vue.js.map +1 -1
- package/js/x-modules/search/store/getters/request.getter.js +1 -1
- package/js/x-modules/search/store/getters/request.getter.js.map +1 -1
- package/js/x-modules/search/store/module.js +1 -0
- package/js/x-modules/search/store/module.js.map +1 -1
- package/package.json +24 -24
- package/report/tsdoc-metadata.json +1 -1
- package/report/x-adapter-platform.api.json +5 -5
- package/report/x-components.api.json +830 -659
- package/report/x-components.api.md +40 -5
- package/report/x-types.api.json +30 -30
- package/types/src/components/result/base-result-image.vue.d.ts +105 -6
- package/types/src/components/result/base-result-image.vue.d.ts.map +1 -1
- package/types/src/x-modules/search/config.types.d.ts +5 -0
- package/types/src/x-modules/search/config.types.d.ts.map +1 -1
- package/types/src/x-modules/search/store/module.d.ts.map +1 -1
|
@@ -38,7 +38,7 @@ function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
|
|
|
38
38
|
_: 2
|
|
39
39
|
/* DYNAMIC */
|
|
40
40
|
}, 1032, ["class", "aria-pressed", "events", "aria-label"]),
|
|
41
|
-
index !== _ctx.columnsWithCssClasses.length - 1 ? renderSlot(_ctx.$slots, "divider", {
|
|
41
|
+
index !== _ctx.columnsWithCssClasses.length - 1 ? renderSlot(_ctx.$slots, "divider", {}, void 0, void 0, 0) : createCommentVNode("v-if", true)
|
|
42
42
|
],
|
|
43
43
|
64
|
|
44
44
|
/* STABLE_FRAGMENT */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base-column-picker-list.vue.js","sources":["../../../../src/components/column-picker/base-column-picker-list.vue"],"sourcesContent":["<template>\n <div class=\"x-column-picker-list xds:button-group\" data-test=\"column-picker-list\" role=\"list\">\n <template\n v-for=\"({ column, cssClasses, events, isSelected }, index) in columnsWithCssClasses\"\n :key=\"column\"\n >\n <BaseEventButton\n class=\"x-column-picker-list__button xds:button\"\n :class=\"[buttonClass, cssClasses]\"\n data-test=\"column-picker-button\"\n :aria-pressed=\"isSelected.toString()\"\n :events=\"events\"\n :aria-label=\"ariaLabels?.button!(column)\"\n role=\"listitem\"\n >\n <!--\n @slot Customized Column Picker Button content. Specifying a slot with the column value\n will result in the column using that slot composition to render.\n @binding {number} column - Columns Number to pick.\n @binding {boolean} isSelected - True if the columns number are the chosen value.\n -->\n <slot v-bind=\"{ column, isSelected }\">\n {{ column }}\n </slot>\n </BaseEventButton>\n\n <!--\n @slot Customized Column Picker divider. Specify an element to act as divider for\n the items in the column picker. Empty by default.\n -->\n <slot v-if=\"index !== columnsWithCssClasses.length - 1\" name=\"divider\"></slot>\n </template>\n </div>\n</template>\n\n<script lang=\"ts\">\nimport type { PropType } from 'vue'\nimport type { VueCSSClasses } from '../../utils/types'\nimport type { XEventsTypes } from '../../wiring'\nimport { computed, defineComponent, onBeforeMount, ref, watch } from 'vue'\nimport { useAccessibilityLabels } from '../../composables'\nimport { use$x } from '../../composables/use-$x'\nimport BaseEventButton from '../base-event-button.vue'\n\nexport interface ColumnPickerItem {\n column: number\n cssClasses: VueCSSClasses\n events: Partial<XEventsTypes>\n isSelected: boolean\n}\n\n/**\n * Column picker list component renders a list of buttons to choose the columns number.\n *\n * Additionally, this component exposes the following props to modify the classes of the\n * elements: `buttonClass`.\n *\n * @public\n */\nexport default defineComponent({\n name: 'BaseColumnPickerList',\n components: { BaseEventButton },\n props: {\n /** An array of numbers that represents the number of columns to render. */\n columns: {\n type: Array as PropType<number[]>,\n required: true,\n },\n /** The value of the selected columns number. */\n modelValue: Number,\n /** Class inherited by each button. */\n buttonClass: String,\n },\n emits: ['update:modelValue'],\n setup(props, { emit }) {\n const $x = use$x()\n\n const ariaLabels = useAccessibilityLabels('baseColumnPickerList')\n\n const providedSelectedColumns = computed(() => props.modelValue ?? props.columns[0])\n const selectedColumns = ref(providedSelectedColumns.value)\n\n /**\n * Assigns `selectedColumns` value and emits `ColumnsNumberProvided`.\n *\n * @param column - Column number provided.\n */\n function emitColumnsNumberProvided(column: number) {\n selectedColumns.value = column\n $x.emit('ColumnsNumberProvided', column)\n }\n\n /**\n * Emits `update:modelValue` with the column selected.\n *\n * @param column - Column number selected.\n */\n function emitUpdateModelValue(column: number) {\n if (props.modelValue !== column) {\n emit('update:modelValue', column)\n }\n }\n\n watch(providedSelectedColumns, emitColumnsNumberProvided)\n watch(selectedColumns, emitUpdateModelValue)\n\n $x.on('ColumnsNumberProvided', false).subscribe(column => (selectedColumns.value = column))\n\n /**\n * Synchronizes the columns number before mounting the component. If the real number of selected\n * columns equals the provided columns, it emits the event to sync it with every other component.\n * If it is not equal it means that the user has already selected a number of columns, so we emit\n * a `update:modelValue` event so developers can sync the provided value.\n */\n onBeforeMount(() => {\n if (selectedColumns.value === providedSelectedColumns.value) {\n emitColumnsNumberProvided(selectedColumns.value)\n } else {\n emitUpdateModelValue(selectedColumns.value)\n }\n })\n\n /**\n * Maps the column to an object containing: the `column` and `CSS classes`.\n *\n * @returns An array of objects containing the column number and CSS classes.\n */\n const columnsWithCssClasses = computed<ColumnPickerItem[]>(() =>\n props.columns.map(column => ({\n column,\n cssClasses: [\n `x-column-picker-list__button--${column}-cols`,\n { 'xds:selected': selectedColumns.value === column },\n ],\n isSelected: selectedColumns.value === column,\n events: {\n UserClickedColumnPicker: column,\n ColumnsNumberProvided: column,\n },\n })),\n )\n\n return { ariaLabels, columnsWithCssClasses }\n },\n})\n</script>\n\n<docs lang=\"mdx\">\n## Examples\n\nThis component renders a list of elements in different slots depending on the columns prop. Each\nbutton emits the needed events to sync other instances of column pickers or grids with the\nnumber of columns being selected when clicked.\n\n### Default usage\n\nIt is required to send the columns prop.\n\n```vue live\n<template>\n <BaseColumnPickerList :columns=\"columns\" />\n</template>\n\n<script setup>\nimport { ref } from 'vue'\nimport BaseColumnPickerList from '@empathyco/x-components/js/components/column-picker/base-column-picker-list.vue'\n\nconst columns = ref([2, 4, 6])\n</script>\n```\n\n### Using v-model\n\nIt is possible to do two-way binding in order to synchronize the value with the parent. It will be\nupdated if the value changes or if the parent changes it.\n\n```vue live\n<template>\n <BaseColumnPickerList :columns=\"columns\" v-model=\"selectedColumns\" />\n</template>\n\n<script setup>\nimport { ref } from 'vue'\nimport BaseColumnPickerList from '@empathyco/x-components/js/components/column-picker/base-column-picker-list.vue'\n\nconst columns = ref([2, 4, 6])\nconst selectedColumns = ref(4)\n</script>\n```\n\n### Customized usage\n\n#### Overriding the slots\n\nIt is possible to override the column picker button content.\n\n```vue live\n<template>\n <BaseColumnPickerList :columns=\"columns\">\n <template #default=\"{ column, isSelected }\">\n <span>{{ column }} {{ isSelected ? '🟢' : '' }}</span>\n </template>\n </BaseColumnPickerList>\n</template>\n\n<script setup>\nimport { ref } from 'vue'\nimport BaseColumnPickerList from '@empathyco/x-components/js/components/column-picker/base-column-picker-list.vue'\n\nconst columns = ref([2, 4, 6])\n</script>\n```\n\nIt is also possible to add a divider element between the column picker buttons by overriding the\n`divider` slot.\n\n```vue live\n<template>\n <BaseColumnPickerList :columns=\"columns\">\n <template #divider>\n <ChevronRightIcon aria-hidden=\"true\" />\n </template>\n </BaseColumnPickerList>\n</template>\n\n<script setup>\nimport { ref } from 'vue'\nimport BaseColumnPickerList from '@empathyco/x-components/js/components/column-picker/base-column-picker-list.vue'\nimport ChevronRightIcon from '@empathyco/x-components/js/components/icons/chevron-right.vue'\n\nconst columns = ref([2, 4, 6])\n</script>\n```\n\n#### Customizing the buttons with classes\n\nThe `buttonClass` prop can be used to add classes to the buttons.\n\n```vue live\n<template>\n <BaseColumnPickerList :columns=\"columns\" buttonClass=\"xds:button-circle\" />\n</template>\n\n<script setup>\nimport { ref } from 'vue'\nimport BaseColumnPickerList from '@empathyco/x-components/js/components/column-picker/base-column-picker-list.vue'\n\nconst columns = ref([2, 4, 6])\n</script>\n```\n\n## Events\n\nA list of events that the component will emit:\n\n- [`UserClickedColumnPicker`](https://github.com/empathyco/x/blob/main/packages/x-components/src/wiring/events.types.ts):\n the event is emitted after the user clicks an item. The event payload is the number of columns\n that the clicked item represents.\n- [`ColumnsNumberProvided`](https://github.com/empathyco/x/blob/main/packages/x-components/src/wiring/events.types.ts):\n the event is emitted on component mount. The event payload is the current `selectedColumns` value.\n</docs>\n"],"names":["_openBlock","_createElementBlock","_Fragment","_renderList","_createVNode","_normalizeClass","_renderSlot","_mergeProps","_createTextVNode","_createCommentVNode"],"mappings":";;;;;EACO,KAAA,EAAM,uCAAA;AAAA,EAAwC,WAAA,EAAU,oBAAA;AAAA,EAAqB,IAAA,EAAK;;;;AAAvF,EAAA,OAAAA,SAAA,EAAA,EAAAC,kBAAA,CA+BM,OA/BN,UAAA,EA+BM;AAAA,KAAAD,SAAA,CAAA,IAAA,CAAA,EA9BJC,kBAAA;AAAA,MA6BWC,QAAA;AAAA,MAAA,IAAA;AAAA,MAAAC,UAAA,CA5BqD,4BAAqB,CAAA,EAAzE,MAAA,EAAQ,YAAY,MAAA,EAAQ,UAAA,IAAc,KAAA,KAAK;;;iBACnD,MAAA,EAAM;AAAA,UAAA;AAAA,YAEZC,WAAA,CAkBkB,0BAAA,EAAA;AAAA,cAjBhB,KAAA,EAAKC,cAAA,CAAA,CAAC,yCAAA,EAAyC,CACtC,kBAAa,UAAU,CAAA,CAAA,CAAA;AAAA,cAChC,WAAA,EAAU,sBAAA;AAAA,cACT,cAAA,EAAc,WAAW,QAAA,EAAQ;AAAA,cACjC,MAAA;AAAA,cACA,YAAA,EAAY,IAAA,CAAA,UAAA,EAAY,MAAA,CAAQ,MAAM,CAAA;AAAA,cACvC,IAAA,EAAK;AAAA,aAAA,EAAA;+BAQL,MAEO;AAAA,gBAFPC,WAEO,IAAA,CAAA,MAAA,EAAA,SAAA,EAFPC,UAAA,CAEO,qBAFS,MAAA,EAAQ,UAAA,KAAxB,MAEO;AAAA,kBAAAC,eAAA;oCADF,MAAM,CAAA;AAAA,oBAAA;AAAA;AAAA;AAAA,iBAAA;;;;;YAQD,KAAA,KAAU,IAAA,CAAA,qBAAA,CAAsB,MAAA,GAAM,CAAA,GAAlDF,UAAA,CAA8E,IAAA,CAAA,MAAA,EAAA,SAAA,EAAA,EAAA,
|
|
1
|
+
{"version":3,"file":"base-column-picker-list.vue.js","sources":["../../../../src/components/column-picker/base-column-picker-list.vue"],"sourcesContent":["<template>\n <div class=\"x-column-picker-list xds:button-group\" data-test=\"column-picker-list\" role=\"list\">\n <template\n v-for=\"({ column, cssClasses, events, isSelected }, index) in columnsWithCssClasses\"\n :key=\"column\"\n >\n <BaseEventButton\n class=\"x-column-picker-list__button xds:button\"\n :class=\"[buttonClass, cssClasses]\"\n data-test=\"column-picker-button\"\n :aria-pressed=\"isSelected.toString()\"\n :events=\"events\"\n :aria-label=\"ariaLabels?.button!(column)\"\n role=\"listitem\"\n >\n <!--\n @slot Customized Column Picker Button content. Specifying a slot with the column value\n will result in the column using that slot composition to render.\n @binding {number} column - Columns Number to pick.\n @binding {boolean} isSelected - True if the columns number are the chosen value.\n -->\n <slot v-bind=\"{ column, isSelected }\">\n {{ column }}\n </slot>\n </BaseEventButton>\n\n <!--\n @slot Customized Column Picker divider. Specify an element to act as divider for\n the items in the column picker. Empty by default.\n -->\n <slot v-if=\"index !== columnsWithCssClasses.length - 1\" name=\"divider\"></slot>\n </template>\n </div>\n</template>\n\n<script lang=\"ts\">\nimport type { PropType } from 'vue'\nimport type { VueCSSClasses } from '../../utils/types'\nimport type { XEventsTypes } from '../../wiring'\nimport { computed, defineComponent, onBeforeMount, ref, watch } from 'vue'\nimport { useAccessibilityLabels } from '../../composables'\nimport { use$x } from '../../composables/use-$x'\nimport BaseEventButton from '../base-event-button.vue'\n\nexport interface ColumnPickerItem {\n column: number\n cssClasses: VueCSSClasses\n events: Partial<XEventsTypes>\n isSelected: boolean\n}\n\n/**\n * Column picker list component renders a list of buttons to choose the columns number.\n *\n * Additionally, this component exposes the following props to modify the classes of the\n * elements: `buttonClass`.\n *\n * @public\n */\nexport default defineComponent({\n name: 'BaseColumnPickerList',\n components: { BaseEventButton },\n props: {\n /** An array of numbers that represents the number of columns to render. */\n columns: {\n type: Array as PropType<number[]>,\n required: true,\n },\n /** The value of the selected columns number. */\n modelValue: Number,\n /** Class inherited by each button. */\n buttonClass: String,\n },\n emits: ['update:modelValue'],\n setup(props, { emit }) {\n const $x = use$x()\n\n const ariaLabels = useAccessibilityLabels('baseColumnPickerList')\n\n const providedSelectedColumns = computed(() => props.modelValue ?? props.columns[0])\n const selectedColumns = ref(providedSelectedColumns.value)\n\n /**\n * Assigns `selectedColumns` value and emits `ColumnsNumberProvided`.\n *\n * @param column - Column number provided.\n */\n function emitColumnsNumberProvided(column: number) {\n selectedColumns.value = column\n $x.emit('ColumnsNumberProvided', column)\n }\n\n /**\n * Emits `update:modelValue` with the column selected.\n *\n * @param column - Column number selected.\n */\n function emitUpdateModelValue(column: number) {\n if (props.modelValue !== column) {\n emit('update:modelValue', column)\n }\n }\n\n watch(providedSelectedColumns, emitColumnsNumberProvided)\n watch(selectedColumns, emitUpdateModelValue)\n\n $x.on('ColumnsNumberProvided', false).subscribe(column => (selectedColumns.value = column))\n\n /**\n * Synchronizes the columns number before mounting the component. If the real number of selected\n * columns equals the provided columns, it emits the event to sync it with every other component.\n * If it is not equal it means that the user has already selected a number of columns, so we emit\n * a `update:modelValue` event so developers can sync the provided value.\n */\n onBeforeMount(() => {\n if (selectedColumns.value === providedSelectedColumns.value) {\n emitColumnsNumberProvided(selectedColumns.value)\n } else {\n emitUpdateModelValue(selectedColumns.value)\n }\n })\n\n /**\n * Maps the column to an object containing: the `column` and `CSS classes`.\n *\n * @returns An array of objects containing the column number and CSS classes.\n */\n const columnsWithCssClasses = computed<ColumnPickerItem[]>(() =>\n props.columns.map(column => ({\n column,\n cssClasses: [\n `x-column-picker-list__button--${column}-cols`,\n { 'xds:selected': selectedColumns.value === column },\n ],\n isSelected: selectedColumns.value === column,\n events: {\n UserClickedColumnPicker: column,\n ColumnsNumberProvided: column,\n },\n })),\n )\n\n return { ariaLabels, columnsWithCssClasses }\n },\n})\n</script>\n\n<docs lang=\"mdx\">\n## Examples\n\nThis component renders a list of elements in different slots depending on the columns prop. Each\nbutton emits the needed events to sync other instances of column pickers or grids with the\nnumber of columns being selected when clicked.\n\n### Default usage\n\nIt is required to send the columns prop.\n\n```vue live\n<template>\n <BaseColumnPickerList :columns=\"columns\" />\n</template>\n\n<script setup>\nimport { ref } from 'vue'\nimport BaseColumnPickerList from '@empathyco/x-components/js/components/column-picker/base-column-picker-list.vue'\n\nconst columns = ref([2, 4, 6])\n</script>\n```\n\n### Using v-model\n\nIt is possible to do two-way binding in order to synchronize the value with the parent. It will be\nupdated if the value changes or if the parent changes it.\n\n```vue live\n<template>\n <BaseColumnPickerList :columns=\"columns\" v-model=\"selectedColumns\" />\n</template>\n\n<script setup>\nimport { ref } from 'vue'\nimport BaseColumnPickerList from '@empathyco/x-components/js/components/column-picker/base-column-picker-list.vue'\n\nconst columns = ref([2, 4, 6])\nconst selectedColumns = ref(4)\n</script>\n```\n\n### Customized usage\n\n#### Overriding the slots\n\nIt is possible to override the column picker button content.\n\n```vue live\n<template>\n <BaseColumnPickerList :columns=\"columns\">\n <template #default=\"{ column, isSelected }\">\n <span>{{ column }} {{ isSelected ? '🟢' : '' }}</span>\n </template>\n </BaseColumnPickerList>\n</template>\n\n<script setup>\nimport { ref } from 'vue'\nimport BaseColumnPickerList from '@empathyco/x-components/js/components/column-picker/base-column-picker-list.vue'\n\nconst columns = ref([2, 4, 6])\n</script>\n```\n\nIt is also possible to add a divider element between the column picker buttons by overriding the\n`divider` slot.\n\n```vue live\n<template>\n <BaseColumnPickerList :columns=\"columns\">\n <template #divider>\n <ChevronRightIcon aria-hidden=\"true\" />\n </template>\n </BaseColumnPickerList>\n</template>\n\n<script setup>\nimport { ref } from 'vue'\nimport BaseColumnPickerList from '@empathyco/x-components/js/components/column-picker/base-column-picker-list.vue'\nimport ChevronRightIcon from '@empathyco/x-components/js/components/icons/chevron-right.vue'\n\nconst columns = ref([2, 4, 6])\n</script>\n```\n\n#### Customizing the buttons with classes\n\nThe `buttonClass` prop can be used to add classes to the buttons.\n\n```vue live\n<template>\n <BaseColumnPickerList :columns=\"columns\" buttonClass=\"xds:button-circle\" />\n</template>\n\n<script setup>\nimport { ref } from 'vue'\nimport BaseColumnPickerList from '@empathyco/x-components/js/components/column-picker/base-column-picker-list.vue'\n\nconst columns = ref([2, 4, 6])\n</script>\n```\n\n## Events\n\nA list of events that the component will emit:\n\n- [`UserClickedColumnPicker`](https://github.com/empathyco/x/blob/main/packages/x-components/src/wiring/events.types.ts):\n the event is emitted after the user clicks an item. The event payload is the number of columns\n that the clicked item represents.\n- [`ColumnsNumberProvided`](https://github.com/empathyco/x/blob/main/packages/x-components/src/wiring/events.types.ts):\n the event is emitted on component mount. The event payload is the current `selectedColumns` value.\n</docs>\n"],"names":["_openBlock","_createElementBlock","_Fragment","_renderList","_createVNode","_normalizeClass","_renderSlot","_mergeProps","_createTextVNode","_createCommentVNode"],"mappings":";;;;;EACO,KAAA,EAAM,uCAAA;AAAA,EAAwC,WAAA,EAAU,oBAAA;AAAA,EAAqB,IAAA,EAAK;;;;AAAvF,EAAA,OAAAA,SAAA,EAAA,EAAAC,kBAAA,CA+BM,OA/BN,UAAA,EA+BM;AAAA,KAAAD,SAAA,CAAA,IAAA,CAAA,EA9BJC,kBAAA;AAAA,MA6BWC,QAAA;AAAA,MAAA,IAAA;AAAA,MAAAC,UAAA,CA5BqD,4BAAqB,CAAA,EAAzE,MAAA,EAAQ,YAAY,MAAA,EAAQ,UAAA,IAAc,KAAA,KAAK;;;iBACnD,MAAA,EAAM;AAAA,UAAA;AAAA,YAEZC,WAAA,CAkBkB,0BAAA,EAAA;AAAA,cAjBhB,KAAA,EAAKC,cAAA,CAAA,CAAC,yCAAA,EAAyC,CACtC,kBAAa,UAAU,CAAA,CAAA,CAAA;AAAA,cAChC,WAAA,EAAU,sBAAA;AAAA,cACT,cAAA,EAAc,WAAW,QAAA,EAAQ;AAAA,cACjC,MAAA;AAAA,cACA,YAAA,EAAY,IAAA,CAAA,UAAA,EAAY,MAAA,CAAQ,MAAM,CAAA;AAAA,cACvC,IAAA,EAAK;AAAA,aAAA,EAAA;+BAQL,MAEO;AAAA,gBAFPC,WAEO,IAAA,CAAA,MAAA,EAAA,SAAA,EAFPC,UAAA,CAEO,qBAFS,MAAA,EAAQ,UAAA,KAAxB,MAEO;AAAA,kBAAAC,eAAA;oCADF,MAAM,CAAA;AAAA,oBAAA;AAAA;AAAA;AAAA,iBAAA;;;;;YAQD,KAAA,KAAU,IAAA,CAAA,qBAAA,CAAsB,MAAA,GAAM,CAAA,GAAlDF,UAAA,CAA8E,IAAA,CAAA,MAAA,EAAA,SAAA,EAAA,EAAA,EAAA,MAAA,EAAA,MAAA,EAAA,CAAA,CAAA,GAAAG,kBAAA,CAAA,MAAA,EAAA,IAAA;;;;;;;;;;;;;;;"}
|
|
@@ -1,52 +1,55 @@
|
|
|
1
1
|
import _sfc_main from './base-result-image.vue2.js';
|
|
2
|
-
import { openBlock, createElementBlock,
|
|
2
|
+
import { openBlock, createElementBlock, mergeProps, createCommentVNode, createBlock, resolveDynamicComponent, withCtx, renderSlot } from 'vue';
|
|
3
3
|
import './base-result-image.vue3.js';
|
|
4
4
|
import _export_sfc from '../../_virtual/_plugin-vue_export-helper.js';
|
|
5
5
|
|
|
6
|
-
const _hoisted_1 = ["src"];
|
|
7
|
-
const _hoisted_2 = ["alt", "src"];
|
|
6
|
+
const _hoisted_1 = ["loading", "src", "srcset", "sizes"];
|
|
7
|
+
const _hoisted_2 = ["alt", "src", "srcset", "sizes"];
|
|
8
8
|
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
|
|
9
9
|
return openBlock(), createElementBlock(
|
|
10
10
|
"div",
|
|
11
|
-
{
|
|
11
|
+
mergeProps(_ctx.rootAttrs, {
|
|
12
12
|
class: "x-result-picture xds:picture",
|
|
13
13
|
"data-test": "result-picture",
|
|
14
14
|
onMouseenterOnce: _cache[2] || (_cache[2] = ($event) => _ctx.userHasHoveredImage = true),
|
|
15
15
|
onMouseenter: _cache[3] || (_cache[3] = ($event) => _ctx.isHovering = true),
|
|
16
16
|
onMouseleave: _cache[4] || (_cache[4] = ($event) => _ctx.isHovering = false)
|
|
17
|
-
},
|
|
17
|
+
}),
|
|
18
18
|
[
|
|
19
|
-
_ctx.shouldLoadNextImage ? (openBlock(), createElementBlock("img", {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
_ctx.shouldLoadNextImage ? (openBlock(), createElementBlock("img", mergeProps({ key: 0 }, _ctx.imageAttrs, {
|
|
20
|
+
loading: _ctx.loading,
|
|
21
|
+
src: _ctx.pendingImages[0].src,
|
|
22
|
+
srcset: _ctx.pendingImages[0].srcset,
|
|
23
|
+
sizes: _ctx.sizes,
|
|
24
|
+
style: _ctx.loaderStyles,
|
|
24
25
|
class: "xds:picture-image",
|
|
25
26
|
"data-test": "result-picture-loader",
|
|
26
27
|
alt: "",
|
|
27
28
|
role: "presentation",
|
|
28
29
|
onLoad: _cache[0] || (_cache[0] = (...args) => _ctx.flagImageLoaded && _ctx.flagImageLoaded(...args)),
|
|
29
30
|
onError: _cache[1] || (_cache[1] = (...args) => _ctx.flagImageAsFailed && _ctx.flagImageAsFailed(...args))
|
|
30
|
-
}, null,
|
|
31
|
+
}), null, 16, _hoisted_1)) : createCommentVNode("v-if", true),
|
|
31
32
|
(openBlock(), createBlock(resolveDynamicComponent(_ctx.animation), {
|
|
32
33
|
class: "xds:picture-image",
|
|
33
34
|
appear: false
|
|
34
35
|
}, {
|
|
35
36
|
default: withCtx(() => [
|
|
36
|
-
!_ctx.loadedImages.length && !_ctx.pendingImages.length ? renderSlot(_ctx.$slots, "fallback", {
|
|
37
|
+
!_ctx.loadedImages.length && !_ctx.pendingImages.length ? renderSlot(_ctx.$slots, "fallback", {}, void 0, true, 0) : !_ctx.loadedImages.length ? renderSlot(_ctx.$slots, "placeholder", {}, void 0, true, 1) : (openBlock(), createElementBlock("img", mergeProps({ key: 2 }, _ctx.imageAttrs, {
|
|
37
38
|
key: _ctx.imageSrc,
|
|
38
39
|
alt: _ctx.result.name,
|
|
39
40
|
src: _ctx.imageSrc,
|
|
41
|
+
srcset: _ctx.imageSrcset,
|
|
42
|
+
sizes: _ctx.sizes,
|
|
40
43
|
class: "x-result-picture-image",
|
|
41
44
|
"data-test": "result-picture-image"
|
|
42
|
-
}, null,
|
|
45
|
+
}), null, 16, _hoisted_2))
|
|
43
46
|
]),
|
|
44
47
|
_: 3
|
|
45
48
|
/* FORWARDED */
|
|
46
49
|
}))
|
|
47
50
|
],
|
|
48
|
-
|
|
49
|
-
/*
|
|
51
|
+
16
|
|
52
|
+
/* FULL_PROPS */
|
|
50
53
|
);
|
|
51
54
|
}
|
|
52
55
|
var baseResultImage = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-17531567"]]);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base-result-image.vue.js","sources":["../../../../src/components/result/base-result-image.vue"],"sourcesContent":["<template>\n <!-- This is a div because using a picture causes the onload event of the image to fire twice. -->\n <!-- eslint-disable-next-line vuejs-accessibility/mouse-events-have-key-events -->\n <div\n class=\"x-result-picture xds:picture\"\n data-test=\"result-picture\"\n @mouseenter.once=\"userHasHoveredImage = true\"\n @mouseenter=\"isHovering = true\"\n @mouseleave=\"isHovering = false\"\n >\n <img\n v-if=\"shouldLoadNextImage\"\n loading=\"lazy\"\n :src=\"pendingImages[0]\"\n :style=\"loaderStyles\"\n class=\"xds:picture-image\"\n data-test=\"result-picture-loader\"\n alt=\"\"\n role=\"presentation\"\n @load=\"flagImageLoaded\"\n @error=\"flagImageAsFailed\"\n />\n <component :is=\"animation\" class=\"xds:picture-image\" :appear=\"false\">\n <!-- @slot Fallback image content. It will be rendered when all the images failed -->\n <slot v-if=\"!loadedImages.length && !pendingImages.length\" name=\"fallback\" />\n\n <!-- @slot Loading image content. It will be rendered while the real image is not loaded -->\n <slot v-else-if=\"!loadedImages.length\" name=\"placeholder\" />\n\n <img\n v-else\n :key=\"imageSrc\"\n :alt=\"result.name\"\n :src=\"imageSrc\"\n class=\"x-result-picture-image\"\n data-test=\"result-picture-image\"\n />\n </component>\n </div>\n</template>\n\n<script lang=\"ts\">\nimport type { Result } from '@empathyco/x-types'\nimport type { PropType, Ref, StyleValue } from 'vue'\nimport { computed, defineComponent, ref, watch } from 'vue'\nimport { AnimationProp } from '../../types'\nimport { NoAnimation } from '../animations'\n\n/**\n * Component to be reused that renders an `<img>`.\n *\n * @public\n */\nexport default defineComponent({\n name: 'BaseResultImage',\n props: {\n /** (Required) The {@link @empathyco/x-types#Result} information. */\n result: {\n type: Object as PropType<Result>,\n required: true,\n },\n /**\n * Animation to use when switching between the placeholder, the loaded image, or the failed\n * image fallback.\n */\n loadAnimation: {\n type: AnimationProp,\n default: () => NoAnimation,\n },\n /** Animation to use when switching between the loaded image and the hover image. */\n hoverAnimation: {\n type: AnimationProp,\n },\n /**\n * Indicates if the next valid image should be displayed on hover.\n *\n * @public\n */\n showNextImageOnHover: {\n type: Boolean,\n default: false,\n },\n },\n setup(props) {\n /**\n * Copy of the images of the result.\n *\n * It is used as a queue of images to load, once an image loads/fails to load, it is removed\n * from this array.\n *\n * @internal\n */\n const pendingImages: Ref<string[]> = ref([])\n\n /**\n * Contains the images that have been loaded successfully.\n *\n * @internal\n */\n const loadedImages: Ref<string[]> = ref([])\n\n /**\n * Indicates if the user is hovering the image.\n *\n * @internal\n */\n const isHovering = ref(false)\n\n /**\n * Indicates if the user has hovered the image.\n *\n * @internal\n */\n const userHasHoveredImage = ref(false)\n\n /**\n * Styles to use inline in the image loader, to prevent override from CSS\n *\n * @internal\n */\n const loaderStyles: StyleValue = {\n // csstype issue: https://github.com/frenic/csstype/issues/160\n position: 'absolute !important' as 'absolute',\n top: '0 !important' as '0',\n left: '0 !important' as '0',\n width: '100% !important' as '100%',\n height: '100% !important' as '100%',\n pointerEvents: 'none !important' as 'none',\n visibility: 'hidden !important' as 'hidden',\n }\n\n /**\n * Initializes images state and resets when the result's images change.\n *\n * @internal\n */\n watch(\n () => props.result.images,\n () => {\n pendingImages.value = [...(props.result.images ?? [])]\n loadedImages.value = pendingImages.value.filter(image => loadedImages.value.includes(image))\n },\n { immediate: true },\n )\n\n /**\n * Animation to be used.\n *\n * @returns The animation to be used, taking into account if the user has hovered the image.\n *\n * @internal\n */\n const animation = computed(() => {\n return userHasHoveredImage.value\n ? (props.hoverAnimation ?? props.loadAnimation)\n : props.loadAnimation\n })\n\n /**\n * Gets the src from the result image.\n *\n * @returns The result image src.\n *\n * @internal\n */\n const imageSrc = computed(() => {\n return loadedImages.value[\n !props.showNextImageOnHover || !isHovering.value ? 0 : loadedImages.value.length - 1\n ]\n })\n\n /**\n * Indicates if the loader should try to load the next image.\n *\n * @returns True if it should try to load the next image.\n *\n * @internal\n */\n const shouldLoadNextImage = computed(() => {\n const numImagesToLoad = props.showNextImageOnHover && userHasHoveredImage.value ? 2 : 1\n return !!pendingImages.value.length && loadedImages.value.length < numImagesToLoad\n })\n\n /**\n * Sets an image as failed.\n *\n * @internal\n */\n const flagImageAsFailed = () => {\n pendingImages.value.shift()\n }\n\n /**\n * Sets an image as loaded.\n *\n * @internal\n */\n const flagImageLoaded = () => {\n const image = pendingImages.value.shift()\n if (image) {\n loadedImages.value.push(image)\n }\n }\n\n return {\n pendingImages,\n loadedImages,\n isHovering,\n userHasHoveredImage,\n loaderStyles,\n animation,\n imageSrc,\n shouldLoadNextImage,\n flagImageAsFailed,\n flagImageLoaded,\n }\n },\n})\n</script>\n\n<style lang=\"css\" scoped>\n.x-result-picture {\n position: relative;\n min-width: 1px;\n min-height: 1px;\n}\n\n.x-result-picture-image {\n max-width: 100%;\n max-height: 100%;\n}\n</style>\n\n<docs lang=\"mdx\">\n## Examples\n\n### Basic example\n\nThis component is for the result image. It may be part of the search result page, recommendations or\nother section which needs to include results.\n\nThe result prop is required. It will render a `<img/>` with the result image:\n\n```vue\n<template>\n <BaseResultImage :result=\"result\" />\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\nconst result = {\n name: 'Jacket',\n images: ['https://some-image-url.com/image1.jpg'],\n}\n</script>\n```\n\n### Showing the next image on hover\n\nIf a result has multiple images, it can show the next one on hover.\n\n```vue\n<template>\n <BaseResultImage :result=\"result\" showNextImageOnHover />\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\nconst result = {\n name: 'Jacket',\n images: ['https://some-image-url.com/image1.jpg', 'https://some-image-url.com/image2.jpg'],\n}\n</script>\n```\n\n### Customizing slots content\n\nFallback and placeholder contents can be customized.\n\nThe fallback slot allows you to replace the content of the fallback image.\n\nThe other slot is called `placeholder`, and allows you to set the image that its going to be\ndisplayed while the real one is loaded.\n\n```vue\n<template>\n <BaseResultImage :result=\"result\">\n <template #placeholder>\n <img alt=\"Placeholder image\" src=\"./placeholder-image.svg\" />\n </template>\n <template #fallback>\n <img alt=\"Fallback image\" src=\"./fallback-image.svg\" />\n </template>\n </BaseResultImage>\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\nconst result = {\n name: 'Jacket',\n images: ['https://some-image-url.com/image1.jpg'],\n}\n</script>\n```\n\n### Customizing the animations\n\nTwo animations can be used in this component.\n\nThe `loadAnimation` is used to transition between the placeholder, the fallback and the image.\n\nThe `hoverAnimation` is used to transition between the image and the hover image, if the\n`showNextImageOnHover` prop is `true`.\n\n`hoverAnimation` will default to `loadAnimation` if it is not provided.\n\n```vue\n<template>\n <BaseResultImage\n :result=\"result\"\n :loadAnimation=\"loadAnimation\"\n :hoverAnimation=\"hoverAnimation\"\n showNextImageOnHover\n />\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\nimport { CrossFade, CollapseHeight } from '@empathyco/x-components/animations'\nconst loadAnimation = CrossFade\nconst hoverAnimation = CollapseHeight\nconst result = {\n name: 'Jacket',\n images: ['https://some-image-url.com/image1.jpg', 'https://some-image-url.com/image2.jpg'],\n}\n</script>\n```\n</docs>\n"],"names":["_openBlock","_createElementBlock","_normalizeStyle","_createCommentVNode","_createBlock","_resolveDynamicComponent","_renderSlot"],"mappings":";;;;;;;;AAGE,EAAA,OAAAA,SAAA,EAAA,EAAAC,kBAAA;AAAA,IAmCM,KAAA;AAAA,IAAA;AAAA,MAlCJ,KAAA,EAAM,8BAAA;AAAA,MACN,WAAA,EAAU,gBAAA;AAAA,MAAA,gBAAA,EAAA,MAAA,CAAA,CAAA,CAAA,KAAA,MAAA,CAAA,CAAA,CAAA,GAAA,CA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,CAAK,IAAA,CAAA,aAAA,CAAc,MAAA,GAAnDC,UAAA,CAA6E,IAAA,CAAA,MAAA,EAAA,UAAA,EAAA,EAAA,GAAA,EAAA,CAAA,EAAA,EAAA,MAAA,EAAA,IAAA,CAAA,GAAA,CAG3D,IAAA,CAAA,YAAA,CAAa,MAAA,GAA/BA,UAAA,CAA4D,IAAA,CAAA,MAAA,EAAA,aAAA,EAAA,EAAA,GAAA,EAAA,CAAA,EAAA,EAAA,MAAA,EAAA,IAAA,CAAA,IAAAN,SAAA,EAAA,EAE5DC,kBAAA,CAOE,KAAA,EAAA;AAAA,YALC,GAAA,EAAK,IAAA,CAAA,QAAA;AAAA,YACL,KAAK,IAAA,CAAA,MAAA,CAAO,IAAA;AAAA,YACZ,GAAA,EAAK,IAAA,CAAA,QAAA;AAAA,YACN,KAAA,EAAM,wBAAA;AAAA,YACN,WAAA,EAAU;AAAA,WAAA,EAAA,IAAA,EAAA,CAAA,EAAA,UAAA,CAAA;;;;;;;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"base-result-image.vue.js","sources":["../../../../src/components/result/base-result-image.vue"],"sourcesContent":["<template>\n <!-- This is a div because using a picture causes the onload event of the image to fire twice. -->\n <!-- eslint-disable-next-line vuejs-accessibility/mouse-events-have-key-events -->\n <div\n v-bind=\"rootAttrs\"\n class=\"x-result-picture xds:picture\"\n data-test=\"result-picture\"\n @mouseenter.once=\"userHasHoveredImage = true\"\n @mouseenter=\"isHovering = true\"\n @mouseleave=\"isHovering = false\"\n >\n <img\n v-if=\"shouldLoadNextImage\"\n v-bind=\"imageAttrs\"\n :loading=\"loading\"\n :src=\"pendingImages[0].src\"\n :srcset=\"pendingImages[0].srcset\"\n :sizes=\"sizes\"\n :style=\"loaderStyles\"\n class=\"xds:picture-image\"\n data-test=\"result-picture-loader\"\n alt=\"\"\n role=\"presentation\"\n @load=\"flagImageLoaded\"\n @error=\"flagImageAsFailed\"\n />\n <component :is=\"animation\" class=\"xds:picture-image\" :appear=\"false\">\n <!-- @slot Fallback image content. It will be rendered when all the images failed -->\n <slot v-if=\"!loadedImages.length && !pendingImages.length\" name=\"fallback\" />\n\n <!-- @slot Loading image content. It will be rendered while the real image is not loaded -->\n <slot v-else-if=\"!loadedImages.length\" name=\"placeholder\" />\n\n <img\n v-else\n v-bind=\"imageAttrs\"\n :key=\"imageSrc\"\n :alt=\"result.name\"\n :src=\"imageSrc\"\n :srcset=\"imageSrcset\"\n :sizes=\"sizes\"\n class=\"x-result-picture-image\"\n data-test=\"result-picture-image\"\n />\n </component>\n </div>\n</template>\n\n<script lang=\"ts\">\nimport type { Result } from '@empathyco/x-types'\nimport type { PropType, Ref, StyleValue } from 'vue'\nimport { computed, defineComponent, ref, useAttrs, watch } from 'vue'\n\nimport { AnimationProp } from '../../types'\nimport { NoAnimation } from '../animations'\n\n/**\n * An entry of the images queue: the image `src` and its optional `srcset`.\n *\n * @internal\n */\ninterface ImageEntry {\n /** Index of the image inside `result.images`. */\n index: number\n /** The image URL. */\n src: string\n /** Responsive candidates for the image, if provided. */\n srcset?: string\n}\n\n/**\n * Component to be reused that renders an `<img>`.\n *\n * Additional `<img>` attributes passed to the component (e.g. `decoding`, `crossorigin`,\n * `referrerpolicy`, `fetchpriority`, `width` and `height`) are forwarded to the rendered\n * images, while `class`, `style` and event listeners keep falling through to the root\n * element, as before. Use the `imageLoaded` and `imageError` events to observe the image\n * loading state.\n *\n * @public\n */\nexport default defineComponent({\n name: 'BaseResultImage',\n inheritAttrs: false,\n props: {\n /** (Required) The {@link @empathyco/x-types#Result} information. */\n result: {\n type: Object as PropType<Result>,\n required: true,\n },\n /**\n * Animation to use when switching between the placeholder, the loaded image, or the failed\n * image fallback.\n */\n loadAnimation: {\n type: AnimationProp,\n default: () => NoAnimation,\n },\n /** Animation to use when switching between the loaded image and the hover image. */\n hoverAnimation: {\n type: AnimationProp,\n },\n /**\n * Indicates if the next valid image should be displayed on hover.\n *\n * @public\n */\n showNextImageOnHover: {\n type: Boolean,\n default: false,\n },\n /**\n * Responsive image candidates (`srcset`).\n *\n * - A single string is applied to every image of the result (e.g.\n * `\"image-480.jpg 480w, image-1024.jpg 1024w\"`).\n * - An array is parallel to `result.images`, so each image can have its own candidates.\n *\n * When provided, the `src` is kept as a fallback for browsers without `srcset` support.\n *\n * @public\n */\n srcset: {\n type: [String, Array] as PropType<string | (string | undefined)[]>,\n },\n /**\n * The sizes of the image for different page layouts (`sizes`).\n *\n * It is shared by all the images of the result, since it depends on the layout and not on\n * the image. Pass it together with `srcset`, since it is ignored otherwise.\n *\n * @public\n */\n sizes: {\n type: String,\n },\n /**\n * Image loading mode.\n *\n * @public\n */\n loading: {\n type: String as PropType<'lazy' | 'eager'>,\n default: 'lazy',\n },\n },\n emits: ['imageLoaded', 'imageError'],\n setup(props, { emit }) {\n const attrs = useAttrs()\n\n /**\n * Additional `<img>` attributes passed to the component (e.g. `decoding`,\n * `crossorigin`, `fetchpriority`, `width`, `height`), forwarded to the rendered images.\n *\n * Event listeners are excluded so they don't reach the hidden loader: they keep\n * falling through to the root element, and the image loading state is exposed through\n * the `imageLoaded` and `imageError` events.\n *\n * @internal\n */\n const imageAttrs = computed(() =>\n Object.fromEntries(Object.entries(attrs).filter(([key]) => !key.startsWith('on'))),\n )\n\n /**\n * The consumer's `class`, `style` and event listeners, applied to the root element.\n *\n * The component uses `inheritAttrs: false` so the remaining attributes (e.g.\n * `decoding`, `crossorigin`, `fetchpriority`, `width`, `height`) only reach the\n * rendered images through `imageAttrs` and don't leak to the wrapper div.\n *\n * @internal\n */\n const rootAttrs = computed(() => ({\n class: attrs.class,\n style: attrs.style,\n ...Object.fromEntries(Object.entries(attrs).filter(([key]) => key.startsWith('on'))),\n }))\n\n /**\n * Copy of the images of the result.\n *\n * It is used as a queue of images to load, once an image loads/fails to load, it is removed\n * from this array.\n *\n * @internal\n */\n const pendingImages: Ref<ImageEntry[]> = ref([])\n\n /**\n * Contains the images that have been loaded successfully.\n *\n * @internal\n */\n const loadedImages: Ref<ImageEntry[]> = ref([])\n\n /**\n * Indicates if the user is hovering the image.\n *\n * @internal\n */\n const isHovering = ref(false)\n\n /**\n * Indicates if the user has hovered the image.\n *\n * @internal\n */\n const userHasHoveredImage = ref(false)\n\n /**\n * Styles to use inline in the image loader, to prevent override from CSS\n *\n * @internal\n */\n const loaderStyles: StyleValue = {\n // csstype issue: https://github.com/frenic/csstype/issues/160\n position: 'absolute !important' as 'absolute',\n top: '0 !important' as '0',\n left: '0 !important' as '0',\n width: '100% !important' as '100%',\n height: '100% !important' as '100%',\n pointerEvents: 'none !important' as 'none',\n visibility: 'hidden !important' as 'hidden',\n }\n\n /**\n * Resolves the `srcset` to use for the image at the given index.\n *\n * @param index - Index of the image inside `result.images`.\n *\n * @returns The `srcset` for that image, if any.\n *\n * @internal\n */\n const resolveSrcset = (index: number): string | undefined =>\n Array.isArray(props.srcset) ? props.srcset[index] : props.srcset\n\n /**\n * Initializes images state and resets when the result's images change.\n *\n * @internal\n */\n watch(\n () => props.result.images,\n () => {\n pendingImages.value = (props.result.images ?? []).map((src, index) => ({\n index,\n src,\n srcset: resolveSrcset(index),\n }))\n loadedImages.value = pendingImages.value.filter(image =>\n loadedImages.value.some(loadedImage => loadedImage.src === image.src),\n )\n },\n { immediate: true },\n )\n\n /**\n * Animation to be used.\n *\n * @returns The animation to be used, taking into account if the user has hovered the image.\n *\n * @internal\n */\n const animation = computed(() => {\n return userHasHoveredImage.value\n ? (props.hoverAnimation ?? props.loadAnimation)\n : props.loadAnimation\n })\n\n /**\n * Gets the currently loaded image.\n *\n * @returns The currently loaded image entry, or undefined if none has been loaded.\n *\n * @internal\n */\n const currentImage = computed(() => {\n return loadedImages.value[\n !props.showNextImageOnHover || !isHovering.value ? 0 : loadedImages.value.length - 1\n ]\n })\n\n /**\n * Gets the src of the currently loaded image.\n *\n * @returns The current image src.\n *\n * @internal\n */\n const imageSrc = computed(() => currentImage.value?.src)\n\n /**\n * Gets the srcset of the currently loaded image.\n *\n * @returns The current image srcset, if any.\n *\n * @internal\n */\n const imageSrcset = computed(() => currentImage.value?.srcset)\n\n /**\n * Indicates if the loader should try to load the next image.\n *\n * @returns True if it should try to load the next image.\n *\n * @internal\n */\n const shouldLoadNextImage = computed(() => {\n const numImagesToLoad = props.showNextImageOnHover && userHasHoveredImage.value ? 2 : 1\n return !!pendingImages.value.length && loadedImages.value.length < numImagesToLoad\n })\n\n /**\n * Sets an image as failed.\n *\n * @param event - The error event of the loader image.\n *\n * @internal\n */\n const flagImageAsFailed = () => {\n const image = pendingImages.value.shift()\n if (image) {\n emit('imageError', { index: image.index, src: image.src, srcset: image.srcset })\n }\n }\n\n /**\n * Sets an image as loaded.\n *\n * @param event - The load event of the loader image.\n *\n * @internal\n */\n const flagImageLoaded = (event: Event) => {\n const image = pendingImages.value.shift()\n if (image) {\n loadedImages.value.push(image)\n const element = event.target as HTMLImageElement\n emit('imageLoaded', {\n index: image.index,\n src: image.src,\n srcset: image.srcset,\n currentSrc: element.currentSrc || image.src,\n element,\n })\n }\n }\n\n return {\n imageAttrs,\n rootAttrs,\n pendingImages,\n loadedImages,\n isHovering,\n userHasHoveredImage,\n loaderStyles,\n animation,\n imageSrc,\n imageSrcset,\n shouldLoadNextImage,\n flagImageAsFailed,\n flagImageLoaded,\n }\n },\n})\n</script>\n\n<style lang=\"css\" scoped>\n.x-result-picture {\n position: relative;\n min-width: 1px;\n min-height: 1px;\n}\n\n.x-result-picture-image {\n max-width: 100%;\n max-height: 100%;\n}\n</style>\n\n<docs lang=\"mdx\">\n## Examples\n\n### Basic example\n\nThis component is for the result image. It may be part of the search result page, recommendations or\nother section which needs to include results.\n\nThe result prop is required. It will render a `<img/>` with the result image:\n\n```vue\n<template>\n <BaseResultImage :result=\"result\" />\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\nconst result = {\n name: 'Jacket',\n images: ['https://some-image-url.com/image1.jpg'],\n}\n</script>\n```\n\n### Responsive images with `srcset` and `sizes`\n\nThe `srcset` and `sizes` props are used to deliver the right image candidate according to the\ndevice and the layout (like the native `<img>` attributes).\n\n`srcset` can be a single string, applied to all the images of the result, or an array parallel to\n`result.images`, so each image can have its own candidates.\n\n```vue\n<template>\n <BaseResultImage\n :result=\"result\"\n srcset=\"image1-480.jpg?width=400 480w, image1-1024.jpg?width=1024 1024w\"\n sizes=\"(min-width: 600px) 33vw, 100vw\"\n />\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\nconst result = {\n name: 'Jacket',\n images: ['https://some-image-url.com/image1.jpg'],\n}\n</script>\n```\n\nThe `src` of the image is always kept, so it works as a fallback when `srcset` is not supported.\n\n### Showing the next image on hover\n\nIf a result has multiple images, it can show the next one on hover.\n\n```vue\n<template>\n <BaseResultImage :result=\"result\" showNextImageOnHover />\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\nconst result = {\n name: 'Jacket',\n images: ['https://some-image-url.com/image1.jpg', 'https://some-image-url.com/image2.jpg'],\n}\n</script>\n```\n\n### Customizing slots content\n\nFallback and placeholder contents can be customized.\n\nThe fallback slot allows you to replace the content of the fallback image.\n\nThe other slot is called `placeholder`, and allows you to set the image that its going to be\ndisplayed while the real one is loaded.\n\n```vue\n<template>\n <BaseResultImage :result=\"result\">\n <template #placeholder>\n <img alt=\"Placeholder image\" src=\"./placeholder-image.svg\" />\n </template>\n <template #fallback>\n <img alt=\"Fallback image\" src=\"./fallback-image.svg\" />\n </template>\n </BaseResultImage>\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\nconst result = {\n name: 'Jacket',\n images: ['https://some-image-url.com/image1.jpg'],\n}\n</script>\n```\n\n### Customizing the animations\n\nTwo animations can be used in this component.\n\nThe `loadAnimation` is used to transition between the placeholder, the fallback and the image.\n\nThe `hoverAnimation` is used to transition between the image and the hover image, if the\n`showNextImageOnHover` prop is `true`.\n\n`hoverAnimation` will default to `loadAnimation` if it is not provided.\n\n```vue\n<template>\n <BaseResultImage\n :result=\"result\"\n :loadAnimation=\"loadAnimation\"\n :hoverAnimation=\"hoverAnimation\"\n showNextImageOnHover\n />\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\nimport { CrossFade, CollapseHeight } from '@empathyco/x-components/animations'\nconst loadAnimation = CrossFade\nconst hoverAnimation = CollapseHeight\nconst result = {\n name: 'Jacket',\n images: ['https://some-image-url.com/image1.jpg', 'https://some-image-url.com/image2.jpg'],\n}\n</script>\n```\n\n### Optimizing the first result for LCP\n\nBy default, images are loaded lazily. For the first (above-the-fold) result of the search results\npage, the image is usually the **Largest Contentful Paint (LCP)** element: eager-loading it with\na high fetch priority avoids waiting for the lazy-load viewport check and lets the browser start\nthe request immediately.\n\nCombine it with `width` and `height` so the browser can prioritize and reserve the space of the\nLCP element from the start:\n\n```vue\n<template>\n <!-- First, above-the-fold result -->\n <BaseResultImage\n :result=\"firstResult\"\n loading=\"eager\"\n fetchPriority=\"high\"\n :width=\"256\"\n :height=\"371\"\n />\n <!-- All the remaining results keep the default lazy loading -->\n <BaseResultImage v-for=\"result in otherResults\" :key=\"result.id\" :result=\"result\" />\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\n</script>\n```\n\nOnly the above-the-fold results should use `loading=\"eager\"`; keep the rest lazy to avoid\nrequesting more images than needed.\n\n### Avoiding layout shift (CLS)\n\nPassing `width` and `height` (the intrinsic dimensions of the image) helps the browser reserve\nthe right space before the image is loaded, preventing the layout shift caused by the image\nreplacing the placeholder.\n\n```vue\n<template>\n <BaseResultImage :result=\"result\" :width=\"256\" :height=\"371\" />\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\n</script>\n```\n\n### Passing additional image attributes\n\nAny additional `<img>` attribute passed to the component is forwarded to the rendered images\n(the hidden preload image and the visible one), so there is no need for a specific prop per\nattribute:\n\n```vue\n<template>\n <BaseResultImage\n :result=\"result\"\n decoding=\"async\"\n crossorigin=\"anonymous\"\n fetchpriority=\"high\"\n referrerpolicy=\"no-referrer\"\n :width=\"256\"\n :height=\"371\"\n />\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\n</script>\n```\n\n`class`, `style` and event listeners keep being applied to the root element, so the wrapper\ncan still be styled and listened to. Any other additional attribute is only applied to the\nrendered images, never to the wrapper div. Observe the image loading state with the\n`imageLoaded` and `imageError` events instead.\n\n### Image loading events\n\nThe component emits `imageLoaded` when an image finishes loading, and `imageError` when an image\nfails to load (the next one of the queue will be tried).\n\n```vue\n<template>\n <BaseResultImage :result=\"result\" @imageLoaded=\"imageLoaded\" @imageError=\"imageError\" />\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\nconst result = {\n name: 'Jacket',\n images: ['https://some-image-url.com/image1.jpg'],\n}\nconst imageLoaded = ({ index, src, currentSrc }) => {\n // For example, track the candidate actually downloaded by the browser\n console.log(`Image ${index} loaded: ${src} (downloaded ${currentSrc})`)\n}\nconst imageError = ({ index, src }) => {\n console.log(`Image ${index} failed: ${src}`)\n}\n</script>\n```\n</docs>\n"],"names":["_openBlock","_createElementBlock","_mergeProps","_createCommentVNode","_createBlock","_resolveDynamicComponent","_renderSlot"],"mappings":";;;;;;;;AAGE,EAAA,OAAAA,SAAA,EAAA,EAAAC,kBAAA;AAAA,IA0CM,KAAA;AAAA,IA1CNC,UAAA,CA0CM,KAzCI,SAAA,EAAS;AAAA,MACjB,KAAA,EAAM,8BAAA;AAAA,MACN,WAAA,EAAU,gBAAA;AAAA,MAAA,gBAAA,EAAA,MAAA,CAAA,CAAA,CAAA,KAAA,MAAA,CAAA,CAAA,CAAA,GAAA,CAAA,MAAA,KACQ,IAAA,CAAA,mBAAA,GAAmB,IAAA,CAAA;AAAA,MACpC,YAAA,EAAU,sCAAE,IAAA,CAAA,UAAA,GAAU,IAAA,CAAA;AAAA,MACtB,YAAA,EAAU,sCAAE,IAAA,CAAA,UAAA,GAAU,KAAA;AAAA,KAAA,CAAA;;AAGf,MAAA,IAAA,CAAA,mBAAA,IAAAF,SAAA,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AA,EAAA,EAAA,MAAA,EAAA,IAAA,EAAA,CAAA,CAAA,GAAA,CAG3D,IAAA,CAAA,YAAA,CAAa,MAAA,GAA/BA,WAA4D,IAAA,CAAA,MAAA,EAAA,aAAA,EAAA,EAAA,EAAA,MAAA,EAAA,IAAA,EAAA,CAAA,CAAA,IAAAN,SAAA,EAAA,EAE5DC,kBAAA,CAUE,KAAA,EAVFC,UAAA,CAUE,YARQ,IAAA,CAAA,UAAA,EAAU;AAAA,YACjB,GAAA,EAAK,IAAA,CAAA,QAAA;AAAA,YACL,KAAK,IAAA,CAAA,MAAA,CAAO,IAAA;AAAA,YACZ,GAAA,EAAK,IAAA,CAAA,QAAA;AAAA,YACL,MAAA,EAAQ,IAAA,CAAA,WAAA;AAAA,YACR,KAAA,EAAO,IAAA,CAAA,KAAA;AAAA,YACR,KAAA,EAAM,wBAAA;AAAA,YACN,WAAA,EAAU;AAAA,WAAA,CAAA,EAAA,IAAA,EAAA,EAAA,EAAA,UAAA,CAAA;;;;;;;;;;;;;;"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { defineComponent, ref, watch
|
|
1
|
+
import { defineComponent, useAttrs, computed, ref, watch } from 'vue';
|
|
2
2
|
import { AnimationProp } from '../../types/animation-prop.js';
|
|
3
3
|
import '../animations/animate-clip-path.vue2.js';
|
|
4
4
|
import '../animations/animate-scale.vue2.js';
|
|
@@ -24,10 +24,17 @@ import '../animations/staggered-fade-and-slide.vue3.js';
|
|
|
24
24
|
/**
|
|
25
25
|
* Component to be reused that renders an `<img>`.
|
|
26
26
|
*
|
|
27
|
+
* Additional `<img>` attributes passed to the component (e.g. `decoding`, `crossorigin`,
|
|
28
|
+
* `referrerpolicy`, `fetchpriority`, `width` and `height`) are forwarded to the rendered
|
|
29
|
+
* images, while `class`, `style` and event listeners keep falling through to the root
|
|
30
|
+
* element, as before. Use the `imageLoaded` and `imageError` events to observe the image
|
|
31
|
+
* loading state.
|
|
32
|
+
*
|
|
27
33
|
* @public
|
|
28
34
|
*/
|
|
29
35
|
var _sfc_main = defineComponent({
|
|
30
36
|
name: 'BaseResultImage',
|
|
37
|
+
inheritAttrs: false,
|
|
31
38
|
props: {
|
|
32
39
|
/** (Required) The {@link @empathyco/x-types#Result} information. */
|
|
33
40
|
result: {
|
|
@@ -55,8 +62,69 @@ var _sfc_main = defineComponent({
|
|
|
55
62
|
type: Boolean,
|
|
56
63
|
default: false,
|
|
57
64
|
},
|
|
65
|
+
/**
|
|
66
|
+
* Responsive image candidates (`srcset`).
|
|
67
|
+
*
|
|
68
|
+
* - A single string is applied to every image of the result (e.g.
|
|
69
|
+
* `"image-480.jpg 480w, image-1024.jpg 1024w"`).
|
|
70
|
+
* - An array is parallel to `result.images`, so each image can have its own candidates.
|
|
71
|
+
*
|
|
72
|
+
* When provided, the `src` is kept as a fallback for browsers without `srcset` support.
|
|
73
|
+
*
|
|
74
|
+
* @public
|
|
75
|
+
*/
|
|
76
|
+
srcset: {
|
|
77
|
+
type: [String, Array],
|
|
78
|
+
},
|
|
79
|
+
/**
|
|
80
|
+
* The sizes of the image for different page layouts (`sizes`).
|
|
81
|
+
*
|
|
82
|
+
* It is shared by all the images of the result, since it depends on the layout and not on
|
|
83
|
+
* the image. Pass it together with `srcset`, since it is ignored otherwise.
|
|
84
|
+
*
|
|
85
|
+
* @public
|
|
86
|
+
*/
|
|
87
|
+
sizes: {
|
|
88
|
+
type: String,
|
|
89
|
+
},
|
|
90
|
+
/**
|
|
91
|
+
* Image loading mode.
|
|
92
|
+
*
|
|
93
|
+
* @public
|
|
94
|
+
*/
|
|
95
|
+
loading: {
|
|
96
|
+
type: String,
|
|
97
|
+
default: 'lazy',
|
|
98
|
+
},
|
|
58
99
|
},
|
|
59
|
-
|
|
100
|
+
emits: ['imageLoaded', 'imageError'],
|
|
101
|
+
setup(props, { emit }) {
|
|
102
|
+
const attrs = useAttrs();
|
|
103
|
+
/**
|
|
104
|
+
* Additional `<img>` attributes passed to the component (e.g. `decoding`,
|
|
105
|
+
* `crossorigin`, `fetchpriority`, `width`, `height`), forwarded to the rendered images.
|
|
106
|
+
*
|
|
107
|
+
* Event listeners are excluded so they don't reach the hidden loader: they keep
|
|
108
|
+
* falling through to the root element, and the image loading state is exposed through
|
|
109
|
+
* the `imageLoaded` and `imageError` events.
|
|
110
|
+
*
|
|
111
|
+
* @internal
|
|
112
|
+
*/
|
|
113
|
+
const imageAttrs = computed(() => Object.fromEntries(Object.entries(attrs).filter(([key]) => !key.startsWith('on'))));
|
|
114
|
+
/**
|
|
115
|
+
* The consumer's `class`, `style` and event listeners, applied to the root element.
|
|
116
|
+
*
|
|
117
|
+
* The component uses `inheritAttrs: false` so the remaining attributes (e.g.
|
|
118
|
+
* `decoding`, `crossorigin`, `fetchpriority`, `width`, `height`) only reach the
|
|
119
|
+
* rendered images through `imageAttrs` and don't leak to the wrapper div.
|
|
120
|
+
*
|
|
121
|
+
* @internal
|
|
122
|
+
*/
|
|
123
|
+
const rootAttrs = computed(() => ({
|
|
124
|
+
class: attrs.class,
|
|
125
|
+
style: attrs.style,
|
|
126
|
+
...Object.fromEntries(Object.entries(attrs).filter(([key]) => key.startsWith('on'))),
|
|
127
|
+
}));
|
|
60
128
|
/**
|
|
61
129
|
* Copy of the images of the result.
|
|
62
130
|
*
|
|
@@ -99,14 +167,28 @@ var _sfc_main = defineComponent({
|
|
|
99
167
|
pointerEvents: 'none !important',
|
|
100
168
|
visibility: 'hidden !important',
|
|
101
169
|
};
|
|
170
|
+
/**
|
|
171
|
+
* Resolves the `srcset` to use for the image at the given index.
|
|
172
|
+
*
|
|
173
|
+
* @param index - Index of the image inside `result.images`.
|
|
174
|
+
*
|
|
175
|
+
* @returns The `srcset` for that image, if any.
|
|
176
|
+
*
|
|
177
|
+
* @internal
|
|
178
|
+
*/
|
|
179
|
+
const resolveSrcset = (index) => Array.isArray(props.srcset) ? props.srcset[index] : props.srcset;
|
|
102
180
|
/**
|
|
103
181
|
* Initializes images state and resets when the result's images change.
|
|
104
182
|
*
|
|
105
183
|
* @internal
|
|
106
184
|
*/
|
|
107
185
|
watch(() => props.result.images, () => {
|
|
108
|
-
pendingImages.value =
|
|
109
|
-
|
|
186
|
+
pendingImages.value = (props.result.images ?? []).map((src, index) => ({
|
|
187
|
+
index,
|
|
188
|
+
src,
|
|
189
|
+
srcset: resolveSrcset(index),
|
|
190
|
+
}));
|
|
191
|
+
loadedImages.value = pendingImages.value.filter(image => loadedImages.value.some(loadedImage => loadedImage.src === image.src));
|
|
110
192
|
}, { immediate: true });
|
|
111
193
|
/**
|
|
112
194
|
* Animation to be used.
|
|
@@ -121,15 +203,31 @@ var _sfc_main = defineComponent({
|
|
|
121
203
|
: props.loadAnimation;
|
|
122
204
|
});
|
|
123
205
|
/**
|
|
124
|
-
* Gets the
|
|
206
|
+
* Gets the currently loaded image.
|
|
125
207
|
*
|
|
126
|
-
* @returns The
|
|
208
|
+
* @returns The currently loaded image entry, or undefined if none has been loaded.
|
|
127
209
|
*
|
|
128
210
|
* @internal
|
|
129
211
|
*/
|
|
130
|
-
const
|
|
212
|
+
const currentImage = computed(() => {
|
|
131
213
|
return loadedImages.value[!props.showNextImageOnHover || !isHovering.value ? 0 : loadedImages.value.length - 1];
|
|
132
214
|
});
|
|
215
|
+
/**
|
|
216
|
+
* Gets the src of the currently loaded image.
|
|
217
|
+
*
|
|
218
|
+
* @returns The current image src.
|
|
219
|
+
*
|
|
220
|
+
* @internal
|
|
221
|
+
*/
|
|
222
|
+
const imageSrc = computed(() => currentImage.value?.src);
|
|
223
|
+
/**
|
|
224
|
+
* Gets the srcset of the currently loaded image.
|
|
225
|
+
*
|
|
226
|
+
* @returns The current image srcset, if any.
|
|
227
|
+
*
|
|
228
|
+
* @internal
|
|
229
|
+
*/
|
|
230
|
+
const imageSrcset = computed(() => currentImage.value?.srcset);
|
|
133
231
|
/**
|
|
134
232
|
* Indicates if the loader should try to load the next image.
|
|
135
233
|
*
|
|
@@ -144,23 +242,40 @@ var _sfc_main = defineComponent({
|
|
|
144
242
|
/**
|
|
145
243
|
* Sets an image as failed.
|
|
146
244
|
*
|
|
245
|
+
* @param event - The error event of the loader image.
|
|
246
|
+
*
|
|
147
247
|
* @internal
|
|
148
248
|
*/
|
|
149
249
|
const flagImageAsFailed = () => {
|
|
150
|
-
pendingImages.value.shift();
|
|
250
|
+
const image = pendingImages.value.shift();
|
|
251
|
+
if (image) {
|
|
252
|
+
emit('imageError', { index: image.index, src: image.src, srcset: image.srcset });
|
|
253
|
+
}
|
|
151
254
|
};
|
|
152
255
|
/**
|
|
153
256
|
* Sets an image as loaded.
|
|
154
257
|
*
|
|
258
|
+
* @param event - The load event of the loader image.
|
|
259
|
+
*
|
|
155
260
|
* @internal
|
|
156
261
|
*/
|
|
157
|
-
const flagImageLoaded = () => {
|
|
262
|
+
const flagImageLoaded = (event) => {
|
|
158
263
|
const image = pendingImages.value.shift();
|
|
159
264
|
if (image) {
|
|
160
265
|
loadedImages.value.push(image);
|
|
266
|
+
const element = event.target;
|
|
267
|
+
emit('imageLoaded', {
|
|
268
|
+
index: image.index,
|
|
269
|
+
src: image.src,
|
|
270
|
+
srcset: image.srcset,
|
|
271
|
+
currentSrc: element.currentSrc || image.src,
|
|
272
|
+
element,
|
|
273
|
+
});
|
|
161
274
|
}
|
|
162
275
|
};
|
|
163
276
|
return {
|
|
277
|
+
imageAttrs,
|
|
278
|
+
rootAttrs,
|
|
164
279
|
pendingImages,
|
|
165
280
|
loadedImages,
|
|
166
281
|
isHovering,
|
|
@@ -168,6 +283,7 @@ var _sfc_main = defineComponent({
|
|
|
168
283
|
loaderStyles,
|
|
169
284
|
animation,
|
|
170
285
|
imageSrc,
|
|
286
|
+
imageSrcset,
|
|
171
287
|
shouldLoadNextImage,
|
|
172
288
|
flagImageAsFailed,
|
|
173
289
|
flagImageLoaded,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base-result-image.vue2.js","sources":["../../../../src/components/result/base-result-image.vue"],"sourcesContent":["<template>\n <!-- This is a div because using a picture causes the onload event of the image to fire twice. -->\n <!-- eslint-disable-next-line vuejs-accessibility/mouse-events-have-key-events -->\n <div\n class=\"x-result-picture xds:picture\"\n data-test=\"result-picture\"\n @mouseenter.once=\"userHasHoveredImage = true\"\n @mouseenter=\"isHovering = true\"\n @mouseleave=\"isHovering = false\"\n >\n <img\n v-if=\"shouldLoadNextImage\"\n loading=\"lazy\"\n :src=\"pendingImages[0]\"\n :style=\"loaderStyles\"\n class=\"xds:picture-image\"\n data-test=\"result-picture-loader\"\n alt=\"\"\n role=\"presentation\"\n @load=\"flagImageLoaded\"\n @error=\"flagImageAsFailed\"\n />\n <component :is=\"animation\" class=\"xds:picture-image\" :appear=\"false\">\n <!-- @slot Fallback image content. It will be rendered when all the images failed -->\n <slot v-if=\"!loadedImages.length && !pendingImages.length\" name=\"fallback\" />\n\n <!-- @slot Loading image content. It will be rendered while the real image is not loaded -->\n <slot v-else-if=\"!loadedImages.length\" name=\"placeholder\" />\n\n <img\n v-else\n :key=\"imageSrc\"\n :alt=\"result.name\"\n :src=\"imageSrc\"\n class=\"x-result-picture-image\"\n data-test=\"result-picture-image\"\n />\n </component>\n </div>\n</template>\n\n<script lang=\"ts\">\nimport type { Result } from '@empathyco/x-types'\nimport type { PropType, Ref, StyleValue } from 'vue'\nimport { computed, defineComponent, ref, watch } from 'vue'\nimport { AnimationProp } from '../../types'\nimport { NoAnimation } from '../animations'\n\n/**\n * Component to be reused that renders an `<img>`.\n *\n * @public\n */\nexport default defineComponent({\n name: 'BaseResultImage',\n props: {\n /** (Required) The {@link @empathyco/x-types#Result} information. */\n result: {\n type: Object as PropType<Result>,\n required: true,\n },\n /**\n * Animation to use when switching between the placeholder, the loaded image, or the failed\n * image fallback.\n */\n loadAnimation: {\n type: AnimationProp,\n default: () => NoAnimation,\n },\n /** Animation to use when switching between the loaded image and the hover image. */\n hoverAnimation: {\n type: AnimationProp,\n },\n /**\n * Indicates if the next valid image should be displayed on hover.\n *\n * @public\n */\n showNextImageOnHover: {\n type: Boolean,\n default: false,\n },\n },\n setup(props) {\n /**\n * Copy of the images of the result.\n *\n * It is used as a queue of images to load, once an image loads/fails to load, it is removed\n * from this array.\n *\n * @internal\n */\n const pendingImages: Ref<string[]> = ref([])\n\n /**\n * Contains the images that have been loaded successfully.\n *\n * @internal\n */\n const loadedImages: Ref<string[]> = ref([])\n\n /**\n * Indicates if the user is hovering the image.\n *\n * @internal\n */\n const isHovering = ref(false)\n\n /**\n * Indicates if the user has hovered the image.\n *\n * @internal\n */\n const userHasHoveredImage = ref(false)\n\n /**\n * Styles to use inline in the image loader, to prevent override from CSS\n *\n * @internal\n */\n const loaderStyles: StyleValue = {\n // csstype issue: https://github.com/frenic/csstype/issues/160\n position: 'absolute !important' as 'absolute',\n top: '0 !important' as '0',\n left: '0 !important' as '0',\n width: '100% !important' as '100%',\n height: '100% !important' as '100%',\n pointerEvents: 'none !important' as 'none',\n visibility: 'hidden !important' as 'hidden',\n }\n\n /**\n * Initializes images state and resets when the result's images change.\n *\n * @internal\n */\n watch(\n () => props.result.images,\n () => {\n pendingImages.value = [...(props.result.images ?? [])]\n loadedImages.value = pendingImages.value.filter(image => loadedImages.value.includes(image))\n },\n { immediate: true },\n )\n\n /**\n * Animation to be used.\n *\n * @returns The animation to be used, taking into account if the user has hovered the image.\n *\n * @internal\n */\n const animation = computed(() => {\n return userHasHoveredImage.value\n ? (props.hoverAnimation ?? props.loadAnimation)\n : props.loadAnimation\n })\n\n /**\n * Gets the src from the result image.\n *\n * @returns The result image src.\n *\n * @internal\n */\n const imageSrc = computed(() => {\n return loadedImages.value[\n !props.showNextImageOnHover || !isHovering.value ? 0 : loadedImages.value.length - 1\n ]\n })\n\n /**\n * Indicates if the loader should try to load the next image.\n *\n * @returns True if it should try to load the next image.\n *\n * @internal\n */\n const shouldLoadNextImage = computed(() => {\n const numImagesToLoad = props.showNextImageOnHover && userHasHoveredImage.value ? 2 : 1\n return !!pendingImages.value.length && loadedImages.value.length < numImagesToLoad\n })\n\n /**\n * Sets an image as failed.\n *\n * @internal\n */\n const flagImageAsFailed = () => {\n pendingImages.value.shift()\n }\n\n /**\n * Sets an image as loaded.\n *\n * @internal\n */\n const flagImageLoaded = () => {\n const image = pendingImages.value.shift()\n if (image) {\n loadedImages.value.push(image)\n }\n }\n\n return {\n pendingImages,\n loadedImages,\n isHovering,\n userHasHoveredImage,\n loaderStyles,\n animation,\n imageSrc,\n shouldLoadNextImage,\n flagImageAsFailed,\n flagImageLoaded,\n }\n },\n})\n</script>\n\n<style lang=\"css\" scoped>\n.x-result-picture {\n position: relative;\n min-width: 1px;\n min-height: 1px;\n}\n\n.x-result-picture-image {\n max-width: 100%;\n max-height: 100%;\n}\n</style>\n\n<docs lang=\"mdx\">\n## Examples\n\n### Basic example\n\nThis component is for the result image. It may be part of the search result page, recommendations or\nother section which needs to include results.\n\nThe result prop is required. It will render a `<img/>` with the result image:\n\n```vue\n<template>\n <BaseResultImage :result=\"result\" />\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\nconst result = {\n name: 'Jacket',\n images: ['https://some-image-url.com/image1.jpg'],\n}\n</script>\n```\n\n### Showing the next image on hover\n\nIf a result has multiple images, it can show the next one on hover.\n\n```vue\n<template>\n <BaseResultImage :result=\"result\" showNextImageOnHover />\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\nconst result = {\n name: 'Jacket',\n images: ['https://some-image-url.com/image1.jpg', 'https://some-image-url.com/image2.jpg'],\n}\n</script>\n```\n\n### Customizing slots content\n\nFallback and placeholder contents can be customized.\n\nThe fallback slot allows you to replace the content of the fallback image.\n\nThe other slot is called `placeholder`, and allows you to set the image that its going to be\ndisplayed while the real one is loaded.\n\n```vue\n<template>\n <BaseResultImage :result=\"result\">\n <template #placeholder>\n <img alt=\"Placeholder image\" src=\"./placeholder-image.svg\" />\n </template>\n <template #fallback>\n <img alt=\"Fallback image\" src=\"./fallback-image.svg\" />\n </template>\n </BaseResultImage>\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\nconst result = {\n name: 'Jacket',\n images: ['https://some-image-url.com/image1.jpg'],\n}\n</script>\n```\n\n### Customizing the animations\n\nTwo animations can be used in this component.\n\nThe `loadAnimation` is used to transition between the placeholder, the fallback and the image.\n\nThe `hoverAnimation` is used to transition between the image and the hover image, if the\n`showNextImageOnHover` prop is `true`.\n\n`hoverAnimation` will default to `loadAnimation` if it is not provided.\n\n```vue\n<template>\n <BaseResultImage\n :result=\"result\"\n :loadAnimation=\"loadAnimation\"\n :hoverAnimation=\"hoverAnimation\"\n showNextImageOnHover\n />\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\nimport { CrossFade, CollapseHeight } from '@empathyco/x-components/animations'\nconst loadAnimation = CrossFade\nconst hoverAnimation = CollapseHeight\nconst result = {\n name: 'Jacket',\n images: ['https://some-image-url.com/image1.jpg', 'https://some-image-url.com/image2.jpg'],\n}\n</script>\n```\n</docs>\n"],"names":["NoAnimation"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAgDA;;;;AAIE;AACF,gBAAe,eAAe,CAAC;AAC7B,IAAA,IAAI,EAAE,iBAAiB;AACvB,IAAA,KAAK,EAAE;;AAEL,QAAA,MAAM,EAAE;AACN,YAAA,IAAI,EAAE,MAA0B;AAChC,YAAA,QAAQ,EAAE,IAAI;AACf,SAAA;AACD;;;AAGE;AACF,QAAA,aAAa,EAAE;AACb,YAAA,IAAI,EAAE,aAAa;AACnB,YAAA,OAAO,EAAE,MAAMA,WAAW;AAC3B,SAAA;;AAED,QAAA,cAAc,EAAE;AACd,YAAA,IAAI,EAAE,aAAa;AACpB,SAAA;AACD;;;;AAIE;AACF,QAAA,oBAAoB,EAAE;AACpB,YAAA,IAAI,EAAE,OAAO;AACb,YAAA,OAAO,EAAE,KAAK;AACf,SAAA;AACF,KAAA;AACD,IAAA,KAAK,CAAC,KAAK,EAAA;AACT;;;;;;;AAOE;AACF,QAAA,MAAM,aAAa,GAAkB,GAAG,CAAC,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AI,CAAA,GAAI,YAAY,CAAC,KAAK,CAAC,SAAS,CAAA,CACrF;AACF,QAAA,CAAC,CAAA;AAED;;;;;;AAME;AACF,QAAA,MAAM,mBAAkB,GAAI,QAAQ,CAAC,MAAI;AACvC,YAAA,MAAM,eAAc,GAAI,KAAK,CAAC,oBAAmB,IAAK,mBAAmB,CAAC,KAAI,GAAI,IAAI,CAAA;AACtF,YAAA,OAAO,CAAC,CAAC,aAAa,CAAC,KAAK,CAAC,MAAK,IAAK,YAAY,CAAC,KAAK,CAAC,MAAK,GAAI,eAAc;AACnF,QAAA,CAAC,CAAA;AAED;;;;AAIE;QACF,MAAM,iBAAgB,GAAI,MAAI;AAC5B,YAAA,aAAa,CAAC,KAAK,CAAC,KAAK,EAAC;AAC5B,QAAA,CAAA;AAEA;;;;AAIE;QACF,MAAM,kBAAkB,MAAI;YAC1B,MAAM,KAAI,GAAI,aAAa,CAAC,KAAK,CAAC,KAAK,EAAC;YACxC,IAAI,KAAK,EAAE;AACT,gBAAA,YAAY,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAA;YAC/B;AACF,QAAA,CAAA;QAEA,OAAO;YACL,aAAa;YACb,YAAY;YACZ,UAAU;YACV,mBAAmB;YACnB,YAAY;YACZ,SAAS;YACT,QAAQ;YACR,mBAAmB;YACnB,iBAAiB;YACjB,eAAe;SACjB;IACF,CAAC;AACF,CAAA,CAAA;;;;"}
|
|
1
|
+
{"version":3,"file":"base-result-image.vue2.js","sources":["../../../../src/components/result/base-result-image.vue"],"sourcesContent":["<template>\n <!-- This is a div because using a picture causes the onload event of the image to fire twice. -->\n <!-- eslint-disable-next-line vuejs-accessibility/mouse-events-have-key-events -->\n <div\n v-bind=\"rootAttrs\"\n class=\"x-result-picture xds:picture\"\n data-test=\"result-picture\"\n @mouseenter.once=\"userHasHoveredImage = true\"\n @mouseenter=\"isHovering = true\"\n @mouseleave=\"isHovering = false\"\n >\n <img\n v-if=\"shouldLoadNextImage\"\n v-bind=\"imageAttrs\"\n :loading=\"loading\"\n :src=\"pendingImages[0].src\"\n :srcset=\"pendingImages[0].srcset\"\n :sizes=\"sizes\"\n :style=\"loaderStyles\"\n class=\"xds:picture-image\"\n data-test=\"result-picture-loader\"\n alt=\"\"\n role=\"presentation\"\n @load=\"flagImageLoaded\"\n @error=\"flagImageAsFailed\"\n />\n <component :is=\"animation\" class=\"xds:picture-image\" :appear=\"false\">\n <!-- @slot Fallback image content. It will be rendered when all the images failed -->\n <slot v-if=\"!loadedImages.length && !pendingImages.length\" name=\"fallback\" />\n\n <!-- @slot Loading image content. It will be rendered while the real image is not loaded -->\n <slot v-else-if=\"!loadedImages.length\" name=\"placeholder\" />\n\n <img\n v-else\n v-bind=\"imageAttrs\"\n :key=\"imageSrc\"\n :alt=\"result.name\"\n :src=\"imageSrc\"\n :srcset=\"imageSrcset\"\n :sizes=\"sizes\"\n class=\"x-result-picture-image\"\n data-test=\"result-picture-image\"\n />\n </component>\n </div>\n</template>\n\n<script lang=\"ts\">\nimport type { Result } from '@empathyco/x-types'\nimport type { PropType, Ref, StyleValue } from 'vue'\nimport { computed, defineComponent, ref, useAttrs, watch } from 'vue'\n\nimport { AnimationProp } from '../../types'\nimport { NoAnimation } from '../animations'\n\n/**\n * An entry of the images queue: the image `src` and its optional `srcset`.\n *\n * @internal\n */\ninterface ImageEntry {\n /** Index of the image inside `result.images`. */\n index: number\n /** The image URL. */\n src: string\n /** Responsive candidates for the image, if provided. */\n srcset?: string\n}\n\n/**\n * Component to be reused that renders an `<img>`.\n *\n * Additional `<img>` attributes passed to the component (e.g. `decoding`, `crossorigin`,\n * `referrerpolicy`, `fetchpriority`, `width` and `height`) are forwarded to the rendered\n * images, while `class`, `style` and event listeners keep falling through to the root\n * element, as before. Use the `imageLoaded` and `imageError` events to observe the image\n * loading state.\n *\n * @public\n */\nexport default defineComponent({\n name: 'BaseResultImage',\n inheritAttrs: false,\n props: {\n /** (Required) The {@link @empathyco/x-types#Result} information. */\n result: {\n type: Object as PropType<Result>,\n required: true,\n },\n /**\n * Animation to use when switching between the placeholder, the loaded image, or the failed\n * image fallback.\n */\n loadAnimation: {\n type: AnimationProp,\n default: () => NoAnimation,\n },\n /** Animation to use when switching between the loaded image and the hover image. */\n hoverAnimation: {\n type: AnimationProp,\n },\n /**\n * Indicates if the next valid image should be displayed on hover.\n *\n * @public\n */\n showNextImageOnHover: {\n type: Boolean,\n default: false,\n },\n /**\n * Responsive image candidates (`srcset`).\n *\n * - A single string is applied to every image of the result (e.g.\n * `\"image-480.jpg 480w, image-1024.jpg 1024w\"`).\n * - An array is parallel to `result.images`, so each image can have its own candidates.\n *\n * When provided, the `src` is kept as a fallback for browsers without `srcset` support.\n *\n * @public\n */\n srcset: {\n type: [String, Array] as PropType<string | (string | undefined)[]>,\n },\n /**\n * The sizes of the image for different page layouts (`sizes`).\n *\n * It is shared by all the images of the result, since it depends on the layout and not on\n * the image. Pass it together with `srcset`, since it is ignored otherwise.\n *\n * @public\n */\n sizes: {\n type: String,\n },\n /**\n * Image loading mode.\n *\n * @public\n */\n loading: {\n type: String as PropType<'lazy' | 'eager'>,\n default: 'lazy',\n },\n },\n emits: ['imageLoaded', 'imageError'],\n setup(props, { emit }) {\n const attrs = useAttrs()\n\n /**\n * Additional `<img>` attributes passed to the component (e.g. `decoding`,\n * `crossorigin`, `fetchpriority`, `width`, `height`), forwarded to the rendered images.\n *\n * Event listeners are excluded so they don't reach the hidden loader: they keep\n * falling through to the root element, and the image loading state is exposed through\n * the `imageLoaded` and `imageError` events.\n *\n * @internal\n */\n const imageAttrs = computed(() =>\n Object.fromEntries(Object.entries(attrs).filter(([key]) => !key.startsWith('on'))),\n )\n\n /**\n * The consumer's `class`, `style` and event listeners, applied to the root element.\n *\n * The component uses `inheritAttrs: false` so the remaining attributes (e.g.\n * `decoding`, `crossorigin`, `fetchpriority`, `width`, `height`) only reach the\n * rendered images through `imageAttrs` and don't leak to the wrapper div.\n *\n * @internal\n */\n const rootAttrs = computed(() => ({\n class: attrs.class,\n style: attrs.style,\n ...Object.fromEntries(Object.entries(attrs).filter(([key]) => key.startsWith('on'))),\n }))\n\n /**\n * Copy of the images of the result.\n *\n * It is used as a queue of images to load, once an image loads/fails to load, it is removed\n * from this array.\n *\n * @internal\n */\n const pendingImages: Ref<ImageEntry[]> = ref([])\n\n /**\n * Contains the images that have been loaded successfully.\n *\n * @internal\n */\n const loadedImages: Ref<ImageEntry[]> = ref([])\n\n /**\n * Indicates if the user is hovering the image.\n *\n * @internal\n */\n const isHovering = ref(false)\n\n /**\n * Indicates if the user has hovered the image.\n *\n * @internal\n */\n const userHasHoveredImage = ref(false)\n\n /**\n * Styles to use inline in the image loader, to prevent override from CSS\n *\n * @internal\n */\n const loaderStyles: StyleValue = {\n // csstype issue: https://github.com/frenic/csstype/issues/160\n position: 'absolute !important' as 'absolute',\n top: '0 !important' as '0',\n left: '0 !important' as '0',\n width: '100% !important' as '100%',\n height: '100% !important' as '100%',\n pointerEvents: 'none !important' as 'none',\n visibility: 'hidden !important' as 'hidden',\n }\n\n /**\n * Resolves the `srcset` to use for the image at the given index.\n *\n * @param index - Index of the image inside `result.images`.\n *\n * @returns The `srcset` for that image, if any.\n *\n * @internal\n */\n const resolveSrcset = (index: number): string | undefined =>\n Array.isArray(props.srcset) ? props.srcset[index] : props.srcset\n\n /**\n * Initializes images state and resets when the result's images change.\n *\n * @internal\n */\n watch(\n () => props.result.images,\n () => {\n pendingImages.value = (props.result.images ?? []).map((src, index) => ({\n index,\n src,\n srcset: resolveSrcset(index),\n }))\n loadedImages.value = pendingImages.value.filter(image =>\n loadedImages.value.some(loadedImage => loadedImage.src === image.src),\n )\n },\n { immediate: true },\n )\n\n /**\n * Animation to be used.\n *\n * @returns The animation to be used, taking into account if the user has hovered the image.\n *\n * @internal\n */\n const animation = computed(() => {\n return userHasHoveredImage.value\n ? (props.hoverAnimation ?? props.loadAnimation)\n : props.loadAnimation\n })\n\n /**\n * Gets the currently loaded image.\n *\n * @returns The currently loaded image entry, or undefined if none has been loaded.\n *\n * @internal\n */\n const currentImage = computed(() => {\n return loadedImages.value[\n !props.showNextImageOnHover || !isHovering.value ? 0 : loadedImages.value.length - 1\n ]\n })\n\n /**\n * Gets the src of the currently loaded image.\n *\n * @returns The current image src.\n *\n * @internal\n */\n const imageSrc = computed(() => currentImage.value?.src)\n\n /**\n * Gets the srcset of the currently loaded image.\n *\n * @returns The current image srcset, if any.\n *\n * @internal\n */\n const imageSrcset = computed(() => currentImage.value?.srcset)\n\n /**\n * Indicates if the loader should try to load the next image.\n *\n * @returns True if it should try to load the next image.\n *\n * @internal\n */\n const shouldLoadNextImage = computed(() => {\n const numImagesToLoad = props.showNextImageOnHover && userHasHoveredImage.value ? 2 : 1\n return !!pendingImages.value.length && loadedImages.value.length < numImagesToLoad\n })\n\n /**\n * Sets an image as failed.\n *\n * @param event - The error event of the loader image.\n *\n * @internal\n */\n const flagImageAsFailed = () => {\n const image = pendingImages.value.shift()\n if (image) {\n emit('imageError', { index: image.index, src: image.src, srcset: image.srcset })\n }\n }\n\n /**\n * Sets an image as loaded.\n *\n * @param event - The load event of the loader image.\n *\n * @internal\n */\n const flagImageLoaded = (event: Event) => {\n const image = pendingImages.value.shift()\n if (image) {\n loadedImages.value.push(image)\n const element = event.target as HTMLImageElement\n emit('imageLoaded', {\n index: image.index,\n src: image.src,\n srcset: image.srcset,\n currentSrc: element.currentSrc || image.src,\n element,\n })\n }\n }\n\n return {\n imageAttrs,\n rootAttrs,\n pendingImages,\n loadedImages,\n isHovering,\n userHasHoveredImage,\n loaderStyles,\n animation,\n imageSrc,\n imageSrcset,\n shouldLoadNextImage,\n flagImageAsFailed,\n flagImageLoaded,\n }\n },\n})\n</script>\n\n<style lang=\"css\" scoped>\n.x-result-picture {\n position: relative;\n min-width: 1px;\n min-height: 1px;\n}\n\n.x-result-picture-image {\n max-width: 100%;\n max-height: 100%;\n}\n</style>\n\n<docs lang=\"mdx\">\n## Examples\n\n### Basic example\n\nThis component is for the result image. It may be part of the search result page, recommendations or\nother section which needs to include results.\n\nThe result prop is required. It will render a `<img/>` with the result image:\n\n```vue\n<template>\n <BaseResultImage :result=\"result\" />\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\nconst result = {\n name: 'Jacket',\n images: ['https://some-image-url.com/image1.jpg'],\n}\n</script>\n```\n\n### Responsive images with `srcset` and `sizes`\n\nThe `srcset` and `sizes` props are used to deliver the right image candidate according to the\ndevice and the layout (like the native `<img>` attributes).\n\n`srcset` can be a single string, applied to all the images of the result, or an array parallel to\n`result.images`, so each image can have its own candidates.\n\n```vue\n<template>\n <BaseResultImage\n :result=\"result\"\n srcset=\"image1-480.jpg?width=400 480w, image1-1024.jpg?width=1024 1024w\"\n sizes=\"(min-width: 600px) 33vw, 100vw\"\n />\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\nconst result = {\n name: 'Jacket',\n images: ['https://some-image-url.com/image1.jpg'],\n}\n</script>\n```\n\nThe `src` of the image is always kept, so it works as a fallback when `srcset` is not supported.\n\n### Showing the next image on hover\n\nIf a result has multiple images, it can show the next one on hover.\n\n```vue\n<template>\n <BaseResultImage :result=\"result\" showNextImageOnHover />\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\nconst result = {\n name: 'Jacket',\n images: ['https://some-image-url.com/image1.jpg', 'https://some-image-url.com/image2.jpg'],\n}\n</script>\n```\n\n### Customizing slots content\n\nFallback and placeholder contents can be customized.\n\nThe fallback slot allows you to replace the content of the fallback image.\n\nThe other slot is called `placeholder`, and allows you to set the image that its going to be\ndisplayed while the real one is loaded.\n\n```vue\n<template>\n <BaseResultImage :result=\"result\">\n <template #placeholder>\n <img alt=\"Placeholder image\" src=\"./placeholder-image.svg\" />\n </template>\n <template #fallback>\n <img alt=\"Fallback image\" src=\"./fallback-image.svg\" />\n </template>\n </BaseResultImage>\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\nconst result = {\n name: 'Jacket',\n images: ['https://some-image-url.com/image1.jpg'],\n}\n</script>\n```\n\n### Customizing the animations\n\nTwo animations can be used in this component.\n\nThe `loadAnimation` is used to transition between the placeholder, the fallback and the image.\n\nThe `hoverAnimation` is used to transition between the image and the hover image, if the\n`showNextImageOnHover` prop is `true`.\n\n`hoverAnimation` will default to `loadAnimation` if it is not provided.\n\n```vue\n<template>\n <BaseResultImage\n :result=\"result\"\n :loadAnimation=\"loadAnimation\"\n :hoverAnimation=\"hoverAnimation\"\n showNextImageOnHover\n />\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\nimport { CrossFade, CollapseHeight } from '@empathyco/x-components/animations'\nconst loadAnimation = CrossFade\nconst hoverAnimation = CollapseHeight\nconst result = {\n name: 'Jacket',\n images: ['https://some-image-url.com/image1.jpg', 'https://some-image-url.com/image2.jpg'],\n}\n</script>\n```\n\n### Optimizing the first result for LCP\n\nBy default, images are loaded lazily. For the first (above-the-fold) result of the search results\npage, the image is usually the **Largest Contentful Paint (LCP)** element: eager-loading it with\na high fetch priority avoids waiting for the lazy-load viewport check and lets the browser start\nthe request immediately.\n\nCombine it with `width` and `height` so the browser can prioritize and reserve the space of the\nLCP element from the start:\n\n```vue\n<template>\n <!-- First, above-the-fold result -->\n <BaseResultImage\n :result=\"firstResult\"\n loading=\"eager\"\n fetchPriority=\"high\"\n :width=\"256\"\n :height=\"371\"\n />\n <!-- All the remaining results keep the default lazy loading -->\n <BaseResultImage v-for=\"result in otherResults\" :key=\"result.id\" :result=\"result\" />\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\n</script>\n```\n\nOnly the above-the-fold results should use `loading=\"eager\"`; keep the rest lazy to avoid\nrequesting more images than needed.\n\n### Avoiding layout shift (CLS)\n\nPassing `width` and `height` (the intrinsic dimensions of the image) helps the browser reserve\nthe right space before the image is loaded, preventing the layout shift caused by the image\nreplacing the placeholder.\n\n```vue\n<template>\n <BaseResultImage :result=\"result\" :width=\"256\" :height=\"371\" />\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\n</script>\n```\n\n### Passing additional image attributes\n\nAny additional `<img>` attribute passed to the component is forwarded to the rendered images\n(the hidden preload image and the visible one), so there is no need for a specific prop per\nattribute:\n\n```vue\n<template>\n <BaseResultImage\n :result=\"result\"\n decoding=\"async\"\n crossorigin=\"anonymous\"\n fetchpriority=\"high\"\n referrerpolicy=\"no-referrer\"\n :width=\"256\"\n :height=\"371\"\n />\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\n</script>\n```\n\n`class`, `style` and event listeners keep being applied to the root element, so the wrapper\ncan still be styled and listened to. Any other additional attribute is only applied to the\nrendered images, never to the wrapper div. Observe the image loading state with the\n`imageLoaded` and `imageError` events instead.\n\n### Image loading events\n\nThe component emits `imageLoaded` when an image finishes loading, and `imageError` when an image\nfails to load (the next one of the queue will be tried).\n\n```vue\n<template>\n <BaseResultImage :result=\"result\" @imageLoaded=\"imageLoaded\" @imageError=\"imageError\" />\n</template>\n\n<script setup>\nimport { BaseResultImage } from '@empathyco/x-components'\nconst result = {\n name: 'Jacket',\n images: ['https://some-image-url.com/image1.jpg'],\n}\nconst imageLoaded = ({ index, src, currentSrc }) => {\n // For example, track the candidate actually downloaded by the browser\n console.log(`Image ${index} loaded: ${src} (downloaded ${currentSrc})`)\n}\nconst imageError = ({ index, src }) => {\n console.log(`Image ${index} failed: ${src}`)\n}\n</script>\n```\n</docs>\n"],"names":["NoAnimation"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAsEA;;;;;;;;;;AAUE;AACF,gBAAe,eAAe,CAAC;AAC7B,IAAA,IAAI,EAAE,iBAAiB;AACvB,IAAA,YAAY,EAAE,KAAK;AACnB,IAAA,KAAK,EAAE;;AAEL,QAAA,MAAM,EAAE;AACN,YAAA,IAAI,EAAE,MAA0B;AAChC,YAAA,QAAQ,EAAE,IAAI;AACf,SAAA;AACD;;;AAGE;AACF,QAAA,aAAa,EAAE;AACb,YAAA,IAAI,EAAE,aAAa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AG,EAAE,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,CAAC,QAAQ,CAAA;YACjF;AACF,QAAA,CAAA;AAEA;;;;;;AAME;AACF,QAAA,MAAM,eAAc,GAAI,CAAC,KAAY,KAAG;YACtC,MAAM,KAAI,GAAI,aAAa,CAAC,KAAK,CAAC,KAAK,EAAC;YACxC,IAAI,KAAK,EAAE;AACT,gBAAA,YAAY,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAA;AAC7B,gBAAA,MAAM,OAAM,GAAI,KAAK,CAAC,MAAyB;gBAC/C,IAAI,CAAC,aAAa,EAAE;oBAClB,KAAK,EAAE,KAAK,CAAC,KAAK;oBAClB,GAAG,EAAE,KAAK,CAAC,GAAG;oBACd,MAAM,EAAE,KAAK,CAAC,MAAM;AACpB,oBAAA,UAAU,EAAE,OAAO,CAAC,UAAS,IAAK,KAAK,CAAC,GAAG;oBAC3C,OAAO;AACR,iBAAA,CAAA;YACH;AACF,QAAA,CAAA;QAEA,OAAO;YACL,UAAU;YACV,SAAS;YACT,aAAa;YACb,YAAY;YACZ,UAAU;YACV,mBAAmB;YACnB,YAAY;YACZ,SAAS;YACT,QAAQ;YACR,WAAW;YACX,mBAAmB;YACnB,iBAAiB;YACjB,eAAe;SACjB;IACF,CAAC;AACF,CAAA,CAAA;;;;"}
|
|
@@ -25,22 +25,13 @@ function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
|
|
|
25
25
|
class: "x-facets-list__item",
|
|
26
26
|
"data-test": "facets-facet"
|
|
27
27
|
}, [
|
|
28
|
-
_ctx.hasSlot(slotNameById) ? renderSlot(_ctx.$slots, slotNameById, mergeProps({
|
|
29
|
-
key: 0,
|
|
30
|
-
ref_for: true
|
|
31
|
-
}, {
|
|
28
|
+
_ctx.hasSlot(slotNameById) ? renderSlot(_ctx.$slots, slotNameById, mergeProps({ ref_for: true }, {
|
|
32
29
|
facet,
|
|
33
30
|
selectedFilters: _ctx.selectedFiltersByFacet[facetId] || []
|
|
34
|
-
}), void 0, true) : _ctx.hasSlot(slotNameByModelName) ? renderSlot(_ctx.$slots, slotNameByModelName, mergeProps({
|
|
35
|
-
key: 1,
|
|
36
|
-
ref_for: true
|
|
37
|
-
}, {
|
|
31
|
+
}), void 0, true, 0) : _ctx.hasSlot(slotNameByModelName) ? renderSlot(_ctx.$slots, slotNameByModelName, mergeProps({ ref_for: true }, {
|
|
38
32
|
facet,
|
|
39
33
|
selectedFilters: _ctx.selectedFiltersByFacet[facetId] || []
|
|
40
|
-
}), void 0, true) : renderSlot(_ctx.$slots, "default", mergeProps({
|
|
41
|
-
key: 2,
|
|
42
|
-
ref_for: true
|
|
43
|
-
}, {
|
|
34
|
+
}), void 0, true, 1) : renderSlot(_ctx.$slots, "default", mergeProps({ ref_for: true }, {
|
|
44
35
|
facet,
|
|
45
36
|
selectedFilters: _ctx.selectedFiltersByFacet[facetId] || []
|
|
46
37
|
}), () => [
|
|
@@ -49,7 +40,7 @@ function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
|
|
|
49
40
|
1
|
|
50
41
|
/* TEXT */
|
|
51
42
|
)
|
|
52
|
-
], true)
|
|
43
|
+
], true, 2)
|
|
53
44
|
]);
|
|
54
45
|
}),
|
|
55
46
|
128
|