Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(ui5-media-gallery): migrate to jsx template #10614

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
46 changes: 0 additions & 46 deletions packages/fiori/src/MediaGallery.hbs

This file was deleted.

8 changes: 4 additions & 4 deletions packages/fiori/src/MediaGallery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ItemNavigation from "@ui5/webcomponents-base/dist/delegate/ItemNavigation
import ResizeHandler from "@ui5/webcomponents-base/dist/delegate/ResizeHandler.js";
import { isPhone } from "@ui5/webcomponents-base/dist/Device.js";
import MediaRange from "@ui5/webcomponents-base/dist/MediaRange.js";
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
import jsxRenderer from "@ui5/webcomponents-base/dist/renderer/JsxRenderer.js";
import NavigationMode from "@ui5/webcomponents-base/dist/types/NavigationMode.js";
import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
import Button from "@ui5/webcomponents/dist/Button.js";
Expand All @@ -23,13 +23,13 @@ import type MediaGalleryMenuVerticalAlign from "./types/MediaGalleryMenuVertical
import MediaGalleryCss from "./generated/themes/MediaGallery.css.js";

// Template
import MediaGalleryTemplate from "./generated/templates/MediaGalleryTemplate.lit.js";
import MediaGalleryTemplate from "./MediaGalleryTemplate.js";

/**
* Interface for components that can be slotted inside `ui5-media-gallery` as items.
* @public
*/
interface IMediaGalleryItem extends HTMLElement, ITabbable {
interface IMediaGalleryItem extends UI5Element, ITabbable {
selected: boolean,
disabled: boolean,
displayedContent: HTMLElement | null;
Expand Down Expand Up @@ -86,7 +86,7 @@ const COLUMNS_COUNT: Record<string, number> = {
*/
@customElement({
tag: "ui5-media-gallery",
renderer: litRender,
renderer: jsxRenderer,
styles: [MediaGalleryCss],
template: MediaGalleryTemplate,
dependencies: [
Expand Down
20 changes: 0 additions & 20 deletions packages/fiori/src/MediaGalleryItem.hbs

This file was deleted.

13 changes: 8 additions & 5 deletions packages/fiori/src/MediaGalleryItem.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
import jsxRenderer from "@ui5/webcomponents-base/dist/renderer/JsxRenderer.js";
import { isSpace, isEnter } from "@ui5/webcomponents-base/dist/Keys.js";
import { isPhone } from "@ui5/webcomponents-base/dist/Device.js";
import Icon from "@ui5/webcomponents/dist/Icon.js";
Expand All @@ -15,7 +15,7 @@ import type { IMediaGalleryItem } from "./MediaGallery.js";
import MediaGalleryItemCss from "./generated/themes/MediaGalleryItem.css.js";

// Template
import MediaGalleryItemTemplate from "./generated/templates/MediaGalleryItemTemplate.lit.js";
import MediaGalleryItemTemplate from "./MediaGalleryItemTemplate.js";

/**
* @class
Expand All @@ -42,7 +42,7 @@ import MediaGalleryItemTemplate from "./generated/templates/MediaGalleryItemTemp
*/
@customElement({
tag: "ui5-media-gallery-item",
renderer: litRender,
renderer: jsxRenderer,
styles: MediaGalleryItemCss,
template: MediaGalleryItemTemplate,
dependencies: [Icon],
Expand Down Expand Up @@ -145,11 +145,11 @@ class MediaGalleryItem extends UI5Element implements IMediaGalleryItem {

this._monitoredContent = null;
this._monitoredThumbnail = null;
this._interactive = !isPhone();
}

onEnterDOM() {
this._thumbnailDesign = !isPhone();
this._interactive = !isPhone();
this._square = true;
}

Expand Down Expand Up @@ -178,7 +178,10 @@ class MediaGalleryItem extends UI5Element implements IMediaGalleryItem {
}

get effectiveTabIndex() {
return this.disabled ? undefined : this.forcedTabIndex;
if (this.disabled) {
return undefined;
}
return this.forcedTabIndex ? parseInt(this.forcedTabIndex) : undefined;
}

get _showBackgroundIcon() {
Expand Down
21 changes: 21 additions & 0 deletions packages/fiori/src/MediaGalleryItemTemplate.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type MediaGalleryItem from "./MediaGalleryItem.js";
import Icon from "@ui5/webcomponents/dist/Icon.js";

export default function MediaGalleryItemTemplate(this: MediaGalleryItem) {
return (
<div class="ui5-media-gallery-item-root"
tabindex={this.effectiveTabIndex}
data-sap-focus-ref
onKeyDown={this._onkeydown}
onKeyUp={this._onkeyup}
role={this._role}
>
<div class="ui5-media-gallery-item-mask-layer"></div>
<div class="ui5-media-gallery-item-wrapper" style={this.styles.wrapper}>
{this._showBackgroundIcon && <Icon name="background"></Icon>}
Copy link
Member

@ilhan007 ilhan007 Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One new thing around the migration is that we also encourage importing the used icons in the template
and make use of its default export to bind it:

import background from "@ui5/webcomponents-icons/dist/background.js";

<Icon name={background}></Icon>

This way we will avoid icons missed to be imported, because using the hardcoded string does not have relation to the import.

*afterwards, you can remove the import from the MGitem class

{this._useContent && <slot />}
{this._useThumbnail && <slot name="thumbnail" />}
</div>
</div>
);
}
50 changes: 50 additions & 0 deletions packages/fiori/src/MediaGalleryTemplate.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type MediaGallery from "./MediaGallery.js";
import MediaGalleryItem from "./MediaGalleryItem.js";
import Carousel from "@ui5/webcomponents/dist/Carousel.js";
import Button from "@ui5/webcomponents/dist/Button.js";

export default function MediaGalleryTemplate(this: MediaGallery) {
return (
<div class="ui5-media-gallery-root">
<div class="ui5-media-gallery-display-wrapper">
<div class="ui5-media-gallery-display"
onClick={this._onDisplayAreaClick}>
{this._isPhonePlatform
? <Carousel
onNavigate={this._onCarouselNavigate}
hideNavigationArrows
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

really minor - explicitly setting to true is recommended => hideNavigationArrows={true}

>
{this._selectableItems.map(item =>
<slot name={item._individualSlot}></slot>
)}
</Carousel>
: <MediaGalleryItem
_interactive={this.interactiveDisplayArea}
_square={this._shouldHaveSquareDisplay}
tabIndex={this._mainItemTabIndex}
></MediaGalleryItem>
}
</div>
</div>

{this._showThumbnails && <div class="ui5-media-gallery-thumbnails-wrapper">
<ul>
{this._visibleItems.map(item =>
<li id={item.id}
class="ui5-media-gallery-thumbnail"
role="option"
onClick={this._onThumbnailClick}
>
<slot name={item._individualSlot}></slot>
</li>
)}
{this._showOverflowBtn && <li class="ui5-media-gallery-overflow">
<Button onClick={this._onOverflowBtnClick}>
+{this._overflowSize}
</Button>
</li>}
</ul>
</div>}
</div>
);
}
Loading