-
Notifications
You must be signed in to change notification settings - Fork 0
feat(svg): expose SvgBaseDirective to allow custom icon component implementations
#81
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
14cbbb1
feat(svg): expose `SvgBaseDirective` to allow custom icon component i…
mumenthalers 4c985eb
build(release): next version [skip_build]
actions-user aa0032b
feat(svg): enhance `SvgBaseDirective` to prevent race conditions
mumenthalers db4a30d
build(release): next version [skip_build]
actions-user File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,61 @@ | ||
| import { HttpErrorResponse } from '@angular/common/http' | ||
| import { Directive, effect, ElementRef, inject, Renderer2, Signal } from '@angular/core' | ||
| import { Logger } from '@shiftcode/logger' | ||
|
|
||
| import { SvgRegistry } from './svg-registry.service' | ||
|
|
||
| /** | ||
| * Base class for components that display an SVG element inline. | ||
| * The SVG content is directly inlined as a child of the component, so that CSS styles can easily be applied to it. | ||
| */ | ||
| @Directive() | ||
| export abstract class SvgBaseDirective { | ||
| protected readonly elRef = inject<ElementRef<HTMLElement>>(ElementRef) | ||
| protected readonly renderer = inject(Renderer2) | ||
| protected readonly svgRegistry = inject(SvgRegistry) | ||
|
|
||
| protected abstract readonly logger: Logger | ||
| protected abstract data: Signal<{ url: string; attrs?: Record<string, string> }> | ||
|
|
||
| constructor() { | ||
| effect(() => { | ||
| const { url, attrs } = this.data() | ||
| this.getAndSet(url, attrs) | ||
| }) | ||
| } | ||
|
|
||
| private getAndSet(url: string, attrs?: Record<string, string>) { | ||
| this.svgRegistry | ||
| .getFromUrl(url) | ||
| .then(this.getSvgModifyFn(attrs)) | ||
| .then(this.setSvgElement) | ||
| .catch((err: any) => { | ||
| if (err instanceof HttpErrorResponse && err.status === 0) { | ||
| // in case of no internet or a timeout log a warning, we can not do anything about that | ||
| this.logger.warn(`Error retrieving icon for path ${url}, due to no network`, err) | ||
| } else { | ||
| this.logger.error(`Error retrieving icon for path ${url}`, err) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| private readonly getSvgModifyFn = (attrs?: Record<string, string>) => { | ||
| const attrsEntries = attrs ? Object.entries(attrs) : [] | ||
| if (attrsEntries.length === 0) { | ||
| return (svg: SVGElement): SVGElement => svg | ||
| } | ||
|
|
||
| return (svg: SVGElement): SVGElement => { | ||
| for (const [key, val] of attrsEntries) { | ||
| svg.setAttribute(key, val) | ||
| } | ||
| return svg | ||
| } | ||
| } | ||
|
|
||
| private readonly setSvgElement = (svg: SVGElement | null) => { | ||
| // Remove existing child nodes and add the new SVG element. | ||
| this.elRef.nativeElement.innerHTML = '' | ||
| this.renderer.appendChild(this.elRef.nativeElement, svg) | ||
| } | ||
|
mumenthalers marked this conversation as resolved.
Outdated
|
||
| } | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.