From 58474d79188a0b804851f153efc281c5f160df5a Mon Sep 17 00:00:00 2001 From: TarunAdobe Date: Thu, 6 Nov 2025 14:31:27 +0530 Subject: [PATCH] chore: migrate asset --- .../core/components/asset/Asset.base.ts | 55 ++++++++++++- .../core/components/asset/Asset.types.ts | 15 ++++ .../packages/core/components/asset/index.ts | 1 + .../packages/swc/components/asset/Asset.ts | 53 +++++++++--- .../packages/swc/components/asset/asset.css | 80 +++++-------------- .../components/asset/stories/asset.stories.ts | 74 +++++++++++++---- package.json | 2 +- 7 files changed, 189 insertions(+), 91 deletions(-) create mode 100644 2nd-gen/packages/core/components/asset/Asset.types.ts diff --git a/2nd-gen/packages/core/components/asset/Asset.base.ts b/2nd-gen/packages/core/components/asset/Asset.base.ts index de8d51a9bb7..f4648587da8 100644 --- a/2nd-gen/packages/core/components/asset/Asset.base.ts +++ b/2nd-gen/packages/core/components/asset/Asset.base.ts @@ -10,14 +10,67 @@ * governing permissions and limitations under the License. */ +import { PropertyValues } from 'lit'; import { property } from 'lit/decorators.js'; import { SpectrumElement } from '@spectrum-web-components/core/shared/base/index.js'; +import { ASSET_VARIANTS, type AssetVariant } from './Asset.types.js'; + +/** + * @slot - The content to render when no `variant` is provided (typically an element) + */ export abstract class AssetBase extends SpectrumElement { + // ───────────────────────── + // API TO OVERRIDE + // ───────────────────────── + + /** + * @internal + * + * A readonly array of all valid variants for the asset. + * + * This is an actual internal property, intended not for customer use + */ + static readonly VARIANTS: readonly AssetVariant[] = ASSET_VARIANTS; + + // ───────────────── + // SHARED API + // ───────────────── + + /** + * The variant of the asset. When not provided, slot content is rendered (e.g., an image). + */ @property({ type: String, reflect: true }) - public variant: 'file' | 'folder' | undefined; + public variant: AssetVariant | undefined; + /** + * Accessible label for the asset’s SVG variant. + */ @property() public label = ''; + + // ────────────────────── + // IMPLEMENTATION + // ────────────────────── + + protected override updated(changes: PropertyValues): void { + super.updated(changes); + if (window.__swc?.DEBUG) { + const constructor = this.constructor as typeof AssetBase; + if ( + typeof this.variant !== 'undefined' && + !constructor.VARIANTS.includes(this.variant) + ) { + window.__swc.warn( + this, + `<${this.localName}> element expects the "variant" attribute to be one of the following:`, + 'https://opensource.adobe.com/spectrum-web-components/components/asset/', + { + issues: [...constructor.VARIANTS], + } + ); + } + } + } } diff --git a/2nd-gen/packages/core/components/asset/Asset.types.ts b/2nd-gen/packages/core/components/asset/Asset.types.ts new file mode 100644 index 00000000000..a0609d4a4f8 --- /dev/null +++ b/2nd-gen/packages/core/components/asset/Asset.types.ts @@ -0,0 +1,15 @@ +/** + * Copyright 2025 Adobe. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ + +export const ASSET_VARIANTS = ['file', 'folder'] as const; + +export type AssetVariant = (typeof ASSET_VARIANTS)[number]; diff --git a/2nd-gen/packages/core/components/asset/index.ts b/2nd-gen/packages/core/components/asset/index.ts index 6e056c33d49..abc233ce7a3 100644 --- a/2nd-gen/packages/core/components/asset/index.ts +++ b/2nd-gen/packages/core/components/asset/index.ts @@ -10,3 +10,4 @@ * governing permissions and limitations under the License. */ export * from './Asset.base.js'; +export * from './Asset.types.js'; diff --git a/2nd-gen/packages/swc/components/asset/Asset.ts b/2nd-gen/packages/swc/components/asset/Asset.ts index 8115e7c992c..cf40b9a9bc7 100644 --- a/2nd-gen/packages/swc/components/asset/Asset.ts +++ b/2nd-gen/packages/swc/components/asset/Asset.ts @@ -11,6 +11,7 @@ */ import { CSSResultArray, html, TemplateResult } from 'lit'; +import { classMap } from 'lit/directives/class-map.js'; import { AssetBase } from '@spectrum-web-components/core/components/asset'; @@ -18,17 +19,17 @@ import styles from './asset.css'; const file = (label: string): TemplateResult => html` @@ -36,37 +37,63 @@ const file = (label: string): TemplateResult => html` const folder = (label: string): TemplateResult => html` `; /** + * Use an asset element to visually represent a file, folder, or image. + * File and folder representations center themselves within the available space. + * Images are contained to the element’s size and centered. + * * @element swc-asset - * @slot - content to be displayed in the asset when an acceptable value for `file` is not present + * @slot - content to be displayed when no `variant` is set (typically an ) + * + * @example + * + * Example image + * + * + * @example + * + * + * @example + * */ export class Asset extends AssetBase { + // ────────────────────────────── + // RENDERING & STYLING + // ────────────────────────────── + public static override get styles(): CSSResultArray { return [styles]; } protected override render(): TemplateResult { - if (this.variant === 'file') { - return file(this.label); - } else if (this.variant === 'folder') { - return folder(this.label); - } - return html` `; + return html` +
+ ${this.variant === 'file' + ? file(this.label) + : this.variant === 'folder' + ? folder(this.label) + : html` `} +
+ `; } } diff --git a/2nd-gen/packages/swc/components/asset/asset.css b/2nd-gen/packages/swc/components/asset/asset.css index 5c757d6ff36..51f58f39a04 100644 --- a/2nd-gen/packages/swc/components/asset/asset.css +++ b/2nd-gen/packages/swc/components/asset/asset.css @@ -1,8 +1,9 @@ -/** +/*! * Copyright 2025 Adobe. All rights reserved. + * * This file is licensed to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy - * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * of the License at * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS @@ -10,82 +11,39 @@ * governing permissions and limitations under the License. */ -:host { - --spectrum-asset-folder-background: var( - --highcontrast-asset-icon-background-color, - var( - --mod-asset-folder-background-color, - var(--spectrum-asset-folder-background-color) - ) - ); - --spectrum-asset-file-background: var( - --highcontrast-asset-icon-background-color, - var( - --mod-asset-file-background-color, - var(--spectrum-asset-file-background-color) - ) - ); - --spectrum-asset-folder-outline: var( - --mod-asset-icon-outline-color, - var(--spectrum-asset-icon-outline-color) - ); - --spectrum-asset-file-outline: var( - --mod-asset-icon-outline-color, - var(--spectrum-asset-icon-outline-color) - ); - +.spectrum-Asset { + display: flex; + align-items: center; + justify-content: center; inline-size: 100%; block-size: 100%; - justify-content: center; - align-items: center; - display: flex; - - --spectrum-asset-folder-background-color: var( - --system-asset-folder-background-color - ); - --spectrum-asset-file-background-color: var( - --system-asset-file-background-color - ); - --spectrum-asset-icon-outline-color: var(--system-asset-icon-outline-color); } -::slotted(*) { +.spectrum-Asset-image { max-inline-size: 100%; max-block-size: 100%; object-fit: contain; - transition: opacity var(--spectrum-animation-duration-100); } -.file, -.folder { - inline-size: max(48px, min(100%, 80px)); - inline-size: max( - var(--mod-asset-icon-min-width, 48px), - min(100%, var(--mod-asset-icon-max-width, 80px)) - ); +.spectrum-Asset-folder, +.spectrum-Asset-file { + inline-size: clamp(48px, 100%, 80px); block-size: 100%; margin: 20px; - margin: var(--mod-asset-icon-margin, 20px); -} - -.folderBackground { - fill: var(--spectrum-asset-folder-background); } -.fileBackground { - fill: var(--spectrum-asset-file-background); +.spectrum-Asset-folderBackground { + fill: var(--spectrum-gray-200); } -.folderOutline { - fill: var(--spectrum-asset-folder-outline); +.spectrum-Asset-fileBackground { + fill: var(--spectrum-gray-25); } -.fileOutline { - fill: var(--spectrum-asset-file-outline); +.spectrum-Asset-folderOutline { + fill: var(--spectrum-gray-500); } -@media (forced-colors: active) { - :host { - --highcontrast-asset-icon-background-color: currentcolor; - } +.spectrum-Asset-fileOutline { + fill: var(--spectrum-gray-500); } diff --git a/2nd-gen/packages/swc/components/asset/stories/asset.stories.ts b/2nd-gen/packages/swc/components/asset/stories/asset.stories.ts index 0f5b9ce294a..74e92e31eb4 100644 --- a/2nd-gen/packages/swc/components/asset/stories/asset.stories.ts +++ b/2nd-gen/packages/swc/components/asset/stories/asset.stories.ts @@ -11,34 +11,78 @@ */ import { html } from 'lit'; -import type { Meta, StoryObj } from '@storybook/web-components'; +import type { Meta, StoryObj as Story } from '@storybook/web-components'; +import { getStorybookHelpers } from '@wc-toolkit/storybook-helpers'; + +import { Asset } from '@adobe/swc/asset'; import '@adobe/swc/asset'; +// ──────────────── +// METADATA +// ──────────────── + +const { events, args, argTypes, template } = getStorybookHelpers('swc-asset'); + +argTypes.variant = { + ...argTypes.variant, + control: { type: 'select' }, + options: [undefined, ...Asset.VARIANTS], +}; + const meta: Meta = { title: 'Asset', component: 'swc-asset', - argTypes: { - variant: { - control: { type: 'select' }, - options: ['file', 'folder', undefined], + args, + argTypes, + parameters: { + actions: { + handles: events, }, }, + tags: ['migrated'], }; export default meta; -type Story = StoryObj; -// export const Default: Story = { -// render: (args) => html` `, -// }; +// ─────────────── +// STORIES +// ─────────────── + +args['default-slot'] = IMAGE_PLACEHOLDER_STRING(); export const Default: Story = { - render: (args) => html` `, + render: (args) => template(args), +}; - // render: () => html` - // - // Demo Graphic - // - // `, +export const File: Story = { + args: { + variant: 'file', + }, + render: (args) => + html``, + tags: ['!dev'], +}; + +export const Folder: Story = { + args: { + variant: 'folder', + }, + render: (args) => + html``, + tags: ['!dev'], }; + +// ──────────────────────── +// HELPER FUNCTIONS +// ──────────────────────── + +function IMAGE_PLACEHOLDER_STRING(): string { + return `Example image`; +} diff --git a/package.json b/package.json index b7be2013507..d5df269a8fa 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "postinstall": "husky || true && patch-package", "start": "yarn start:1st-gen & yarn start:2nd-gen", "start:1st-gen": "yarn workspace @spectrum-web-components/1st-gen start", - "start:2nd-gen": "yarn workspace @adobe/swc start", + "start:2nd-gen": "yarn workspace @adobe/swc storybook", "test": "yarn test:1st-gen & yarn test:2nd-gen", "test:1st-gen": "yarn workspace @spectrum-web-components/1st-gen test", "test:2nd-gen": "yarn workspace @adobe/swc test",