Skip to content

Commit 8500305

Browse files
committed
chore: migrate asset
1 parent 1691e33 commit 8500305

File tree

6 files changed

+213
-91
lines changed

6 files changed

+213
-91
lines changed

2nd-gen/packages/core/components/asset/Asset.base.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,69 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13+
import { PropertyValues } from 'lit';
1314
import { property } from 'lit/decorators.js';
1415

1516
import { SpectrumElement } from '@spectrum-web-components/core/shared/base/index.js';
1617

18+
import type { AssetVariant } from './Asset.types.js';
19+
20+
/**
21+
* @slot - The content to render when no `variant` is provided (typically an <img> element)
22+
*/
1723
export abstract class AssetBase extends SpectrumElement {
24+
// ─────────────────────────
25+
// API TO OVERRIDE
26+
// ─────────────────────────
27+
28+
/**
29+
* @internal
30+
*
31+
* A readonly array of all valid variants for the asset.
32+
*
33+
* This is an internal property for validations, stories, tests, etc.
34+
* Because S1 and S2 could support different variants, the value of this
35+
* property must be set in each subclass.
36+
*/
37+
static readonly VARIANTS: readonly string[];
38+
39+
// ─────────────────
40+
// SHARED API
41+
// ─────────────────
42+
43+
/**
44+
* The variant of the asset. When not provided, slot content is rendered (e.g., an image).
45+
*/
1846
@property({ type: String, reflect: true })
19-
public variant: 'file' | 'folder' | undefined;
47+
public variant: AssetVariant | undefined;
2048

49+
/**
50+
* Accessible label for the asset’s SVG variant.
51+
*/
2152
@property()
2253
public label = '';
54+
55+
// ──────────────────────
56+
// IMPLEMENTATION
57+
// ──────────────────────
58+
59+
protected override updated(changes: PropertyValues): void {
60+
super.updated(changes);
61+
if (window.__swc?.DEBUG) {
62+
const constructor = this.constructor as typeof AssetBase;
63+
if (
64+
typeof this.variant !== 'undefined' &&
65+
!constructor.VARIANTS.includes(this.variant)
66+
) {
67+
window.__swc.warn(
68+
this,
69+
`<${this.localName}> element expects the "variant" attribute to be one of the following:`,
70+
'https://opensource.adobe.com/spectrum-web-components/components/asset/',
71+
{
72+
issues: [...constructor.VARIANTS],
73+
}
74+
);
75+
}
76+
}
77+
}
2378
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Copyright 2025 Adobe. All rights reserved.
3+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License. You may obtain a copy
5+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under
8+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
* OF ANY KIND, either express or implied. See the License for the specific language
10+
* governing permissions and limitations under the License.
11+
*/
12+
13+
export const ASSET_VARIANTS = ['file', 'folder'] as const;
14+
15+
export type AssetVariant = (typeof ASSET_VARIANTS)[number];

2nd-gen/packages/core/components/asset/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212
export * from './Asset.base.js';
13+
export * from './Asset.types.js';

2nd-gen/packages/swc/components/asset/Asset.ts

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,62 +11,109 @@
1111
*/
1212

1313
import { CSSResultArray, html, TemplateResult } from 'lit';
14+
import { property } from 'lit/decorators.js';
15+
import { classMap } from 'lit/directives/class-map.js';
1416

15-
import { AssetBase } from '@spectrum-web-components/core/components/asset';
17+
import {
18+
ASSET_VARIANTS,
19+
AssetBase,
20+
type AssetVariant,
21+
} from '@spectrum-web-components/core/components/asset';
1622

1723
import styles from './asset.css';
1824

1925
const file = (label: string): TemplateResult => html`
2026
<svg
21-
class="file"
27+
class="spectrum-Asset-file"
2228
role="img"
2329
viewBox="0 0 128 128"
2430
aria-label=${label || 'File'}
2531
>
2632
<path
27-
class="fileBackground"
33+
class="spectrum-Asset-fileBackground"
2834
d="M24,126c-5.5,0-10-4.5-10-10V12c0-5.5,4.5-10,10-10h61.5c2.1,0,4.1,0.8,5.6,2.3l20.5,20.4c1.5,1.5,2.4,3.5,2.4,5.7V116c0,5.5-4.5,10-10,10H24z"
2935
></path>
3036
<path
31-
class="fileOutline"
37+
class="spectrum-Asset-fileOutline"
3238
d="M113.1,23.3L92.6,2.9C90.7,1,88.2,0,85.5,0H24c-6.6,0-12,5.4-12,12v104c0,6.6,5.4,12,12,12h80c6.6,0,12-5.4,12-12V30.4C116,27.8,114.9,25.2,113.1,23.3z M90,6l20.1,20H92c-1.1,0-2-0.9-2-2V6z M112,116c0,4.4-3.6,8-8,8H24c-4.4,0-8-3.6-8-8V12c0-4.4,3.6-8,8-8h61.5c0.2,0,0.3,0,0.5,0v20c0,3.3,2.7,6,6,6h20c0,0.1,0,0.3,0,0.4V116z"
3339
></path>
3440
</svg>
3541
`;
3642

3743
const folder = (label: string): TemplateResult => html`
3844
<svg
39-
class="folder"
45+
class="spectrum-Asset-folder"
4046
role="img"
4147
viewBox="0 0 32 32"
4248
aria-label=${label || 'Folder'}
4349
>
4450
<path
45-
class="folderBackground"
51+
class="spectrum-Asset-folderBackground"
4652
d="M3,29.5c-1.4,0-2.5-1.1-2.5-2.5V5c0-1.4,1.1-2.5,2.5-2.5h10.1c0.5,0,1,0.2,1.4,0.6l3.1,3.1c0.2,0.2,0.4,0.3,0.7,0.3H29c1.4,0,2.5,1.1,2.5,2.5v18c0,1.4-1.1,2.5-2.5,2.5H3z"
4753
></path>
4854
<path
49-
class="folderOutline"
55+
class="spectrum-Asset-folderOutline"
5056
d="M29,6H18.3c-0.1,0-0.2,0-0.4-0.2l-3.1-3.1C14.4,2.3,13.8,2,13.1,2H3C1.3,2,0,3.3,0,5v22c0,1.6,1.3,3,3,3h26c1.7,0,3-1.4,3-3V9C32,7.3,30.7,6,29,6z M31,27c0,1.1-0.9,2-2,2H3c-1.1,0-2-0.9-2-2V7h28c1.1,0,2,0.9,2,2V27z"
5157
></path>
5258
</svg>
5359
`;
5460

5561
/**
62+
* Use an asset element to visually represent a file, folder, or image.
63+
* File and folder representations center themselves within the available space.
64+
* Images are contained to the element’s size and centered.
65+
*
5666
* @element swc-asset
57-
* @slot - content to be displayed in the asset when an acceptable value for `file` is not present
67+
* @slot - content to be displayed when no `variant` is set (typically an <img>)
68+
*
69+
* @example
70+
* <swc-asset style="block-size: 128px">
71+
* <img class="spectrum-Asset-image" src="example.png" alt="Example image" />
72+
* </swc-asset>
73+
*
74+
* @example
75+
* <swc-asset variant="file" style="min-inline-size: 150px; block-size: 128px"></swc-asset>
76+
*
77+
* @example
78+
* <swc-asset variant="folder" style="min-inline-size: 150px; block-size: 128px"></swc-asset>
5879
*/
5980
export class Asset extends AssetBase {
81+
// ────────────────────
82+
// API OVERRIDES
83+
// ────────────────────
84+
85+
/**
86+
* @internal
87+
*/
88+
static override readonly VARIANTS = ASSET_VARIANTS;
89+
90+
/**
91+
* The variant of the asset (S2-only values).
92+
*/
93+
@property({ type: String, reflect: true })
94+
public override variant: AssetVariant | undefined;
95+
96+
// ──────────────────────────────
97+
// RENDERING & STYLING
98+
// ──────────────────────────────
99+
60100
public static override get styles(): CSSResultArray {
61101
return [styles];
62102
}
63103

64104
protected override render(): TemplateResult {
65-
if (this.variant === 'file') {
66-
return file(this.label);
67-
} else if (this.variant === 'folder') {
68-
return folder(this.label);
69-
}
70-
return html` <slot></slot> `;
105+
return html`
106+
<div
107+
class=${classMap({
108+
['spectrum-Asset']: true,
109+
})}
110+
>
111+
${this.variant === 'file'
112+
? file(this.label)
113+
: this.variant === 'folder'
114+
? folder(this.label)
115+
: html` <slot></slot> `}
116+
</div>
117+
`;
71118
}
72119
}
Lines changed: 19 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,49 @@
1-
/**
1+
/*!
22
* Copyright 2025 Adobe. All rights reserved.
3+
*
34
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
45
* you may not use this file except in compliance with the License. You may obtain a copy
5-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
* of the License at <http://www.apache.org/licenses/LICENSE-2.0>
67
*
78
* Unless required by applicable law or agreed to in writing, software distributed under
89
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
910
* OF ANY KIND, either express or implied. See the License for the specific language
1011
* governing permissions and limitations under the License.
1112
*/
1213

13-
:host {
14-
--spectrum-asset-folder-background: var(
15-
--highcontrast-asset-icon-background-color,
16-
var(
17-
--mod-asset-folder-background-color,
18-
var(--spectrum-asset-folder-background-color)
19-
)
20-
);
21-
--spectrum-asset-file-background: var(
22-
--highcontrast-asset-icon-background-color,
23-
var(
24-
--mod-asset-file-background-color,
25-
var(--spectrum-asset-file-background-color)
26-
)
27-
);
28-
--spectrum-asset-folder-outline: var(
29-
--mod-asset-icon-outline-color,
30-
var(--spectrum-asset-icon-outline-color)
31-
);
32-
--spectrum-asset-file-outline: var(
33-
--mod-asset-icon-outline-color,
34-
var(--spectrum-asset-icon-outline-color)
35-
);
36-
14+
.spectrum-Asset {
15+
display: flex;
16+
align-items: center;
17+
justify-content: center;
3718
inline-size: 100%;
3819
block-size: 100%;
39-
justify-content: center;
40-
align-items: center;
41-
display: flex;
42-
43-
--spectrum-asset-folder-background-color: var(
44-
--system-asset-folder-background-color
45-
);
46-
--spectrum-asset-file-background-color: var(
47-
--system-asset-file-background-color
48-
);
49-
--spectrum-asset-icon-outline-color: var(--system-asset-icon-outline-color);
5020
}
5121

52-
::slotted(*) {
22+
.spectrum-Asset-image {
5323
max-inline-size: 100%;
5424
max-block-size: 100%;
5525
object-fit: contain;
56-
transition: opacity var(--spectrum-animation-duration-100);
5726
}
5827

59-
.file,
60-
.folder {
61-
inline-size: max(48px, min(100%, 80px));
62-
inline-size: max(
63-
var(--mod-asset-icon-min-width, 48px),
64-
min(100%, var(--mod-asset-icon-max-width, 80px))
65-
);
28+
.spectrum-Asset-folder,
29+
.spectrum-Asset-file {
30+
inline-size: clamp(48px, 100%, 80px);
6631
block-size: 100%;
6732
margin: 20px;
68-
margin: var(--mod-asset-icon-margin, 20px);
69-
}
70-
71-
.folderBackground {
72-
fill: var(--spectrum-asset-folder-background);
7333
}
7434

75-
.fileBackground {
76-
fill: var(--spectrum-asset-file-background);
35+
.spectrum-Asset-folderBackground {
36+
fill: var(--spectrum-gray-200);
7737
}
7838

79-
.folderOutline {
80-
fill: var(--spectrum-asset-folder-outline);
39+
.spectrum-Asset-fileBackground {
40+
fill: var(--spectrum-gray-25);
8141
}
8242

83-
.fileOutline {
84-
fill: var(--spectrum-asset-file-outline);
43+
.spectrum-Asset-folderOutline {
44+
fill: var(--spectrum-gray-500);
8545
}
8646

87-
@media (forced-colors: active) {
88-
:host {
89-
--highcontrast-asset-icon-background-color: currentcolor;
90-
}
47+
.spectrum-Asset-fileOutline {
48+
fill: var(--spectrum-gray-500);
9149
}

0 commit comments

Comments
 (0)