mirror of
https://github.com/bitwarden/browser
synced 2025-12-18 09:13:33 +00:00
[EC-512] Tree shakeable icons (#3427)
* [EC-512] feat: create new icon class * [EC-512] feat: implement protected svgIcon function * [EC-512] feat: use new icon class in component * [EC-512] feat: integrate new icons in application * [EC-512] fix: linting * [EC-512] chore: move report icons to where they are used * [EC-512] chore: add export type explanation
This commit is contained in:
25
libs/components/src/icon/icon.ts
Normal file
25
libs/components/src/icon/icon.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
class Icon {
|
||||
constructor(readonly svg: string) {}
|
||||
}
|
||||
|
||||
// We only export the type to prohibit the creation of Icons without using
|
||||
// the `svgIcon` template literal tag.
|
||||
export type { Icon };
|
||||
|
||||
export function isIcon(icon: unknown): icon is Icon {
|
||||
return icon instanceof Icon;
|
||||
}
|
||||
|
||||
export class DynamicContentNotAllowedError extends Error {
|
||||
constructor() {
|
||||
super("Dynamic content in icons is not allowed due to risk of user-injected XSS.");
|
||||
}
|
||||
}
|
||||
|
||||
export function svgIcon(strings: TemplateStringsArray, ...values: unknown[]): Icon {
|
||||
if (values.length > 0) {
|
||||
throw new DynamicContentNotAllowedError();
|
||||
}
|
||||
|
||||
return new Icon(strings[0]);
|
||||
}
|
||||
Reference in New Issue
Block a user