-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(storybook): icons now work with vite config
- Loading branch information
1 parent
80c48b3
commit 271f74a
Showing
3 changed files
with
99 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
web-components/packages/carbon-web-components/.storybook/preview-head.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<script type="text/javascript"> | ||
window.ShadyDOM = { | ||
force: false, // Set true to test ShadyDOM in Chrome | ||
}; | ||
|
||
document.documentElement.setAttribute('lang', 'en'); | ||
</script> | ||
<style type="text/css"> | ||
.cds-ce-doc--demo-iframe { | ||
width: 100%; | ||
height: 120px; | ||
border: 1px solid var(--cds-ui-03, #e0e0e0); | ||
} | ||
</style> | ||
|
||
<link | ||
rel="stylesheet" | ||
href="https://1.www.s81c.com/common/carbon-for-ibm-dotcom/tag/v1/latest/plex.css" /> |
67 changes: 67 additions & 0 deletions
67
web-components/packages/carbon-web-components/tools/vite-svg-result-carbon-icon-loader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* @license | ||
* | ||
* Copyright IBM Corp. 2019, 2023 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
const path = require('path'); | ||
const createSVGResultFromCarbonIcon = require('./svg-result-carbon-icon'); | ||
|
||
/** | ||
* A Vite plugin to generate `lit-html`'s `SVGResult` from an icon descriptor from `@carbon/icons`. | ||
* | ||
* @returns {string} The massaged module content. | ||
*/ | ||
export default function svgResultCarbonIconLoader() { | ||
const svgRegex = /@carbon[\\/]icons[\\/]/i | ||
|
||
const paths = new Map<string, string>() | ||
|
||
return { | ||
name: 'svg-loader', | ||
enforce: 'pre', | ||
|
||
resolveId(id: string): string | null { | ||
if (id.match(svgRegex)) { | ||
paths.set(id, id) | ||
return id | ||
} else { | ||
return null | ||
} | ||
}, | ||
|
||
async load(id: string): Promise<string | undefined> { | ||
let outcome: string | undefined | ||
if (!id.match(svgRegex)) { | ||
return outcome | ||
} | ||
return `` | ||
}, | ||
|
||
async transform(src: string, id: string) { | ||
|
||
if (!paths.has(id)) { | ||
return null | ||
} | ||
|
||
let outcome: string | undefined = src | ||
if (!id.match(svgRegex)) { | ||
return outcome | ||
} | ||
|
||
const descriptor = require(id); | ||
return ` | ||
import { svg } from 'lit'; | ||
import spread from '${path.resolve( | ||
__dirname, | ||
'../src/globals/directives/spread' | ||
)}'; | ||
const svgResultCarbonIcon = ${createSVGResultFromCarbonIcon(descriptor)}; | ||
export default svgResultCarbonIcon; | ||
` | ||
}, | ||
} | ||
} |