2

I have a library that allows you to inject a base-href value for your specific app so that the lib's assets (which you're hosting as an npm dependency in your app) can be accessed without a proxy. This works in most cases, except I can't figure out how to use that injected value in CSS to do this:

$fonts: '#{<injected base path here>}/fonts';

@font-face {
    font-family: 'Roboto';
    font-style: normal;
    font-weight: 300;
    src: local('Roboto Light'), local('Roboto-Light'),
    url('#{$fonts}/roboto-v18-greek-ext_cyrillic-ext_latin-ext-300.woff2') format('woff2'),
    url('#{$fonts}/roboto-v18-greek-ext_cyrillic-ext_latin-ext-300.woff') format('woff');
}

The problem is the URL needs to resolve at build time, so this code won't compile.

2 notes:

  1. I'm using Angular with injection tokens.

  2. In my case, in the scss file, I tried $fonts: '#{var(--asset-base-path)}/fonts'; and in the component.ts file, I tried:

@HostBinding('style.--asset-base-path') assetBasePath = '';

constructor(@Inject(LIB_ASSET_BASE_PATH) path: string) {
    this.assetBasePath = path
}
2
  • there is no string interpolation in css. I believe you should use base href to prepend something to all of your assets/source code, not a library Commented Sep 27, 2024 at 21:20
  • I answered something similar in another question (stackoverflow.com/questions/78790381/…) there is an stackblitz example, perhaps it could help or give you an idea. Commented Sep 27, 2024 at 23:30

1 Answer 1

1

No, This can not be done by DI. DI(Depedency Injection) is a run-time behavior, when Angular initiate a component after application runs, the DI system will create and injecting the depedencies for that component.

CSS Preprocessor such as Sass/Scss/Less will be compiled to pure css files (Since browser only accepts css, not Sass/Scss/Less) and all variables in style files must be evaluated after build. which means you css code has no dynamic behavior at run-time.

The only way to achive your requirement is to use JavaScript to operate styles and set the font path at run-time.

EDIT by OP: See comments and Is it possible to dynamically change font to a custom typeface with javascript?

Sign up to request clarification or add additional context in comments.

3 Comments

Can you explain "use JavaScript to operate styles and set the font path at run-time"? I've used clamping behavior on a component by doing this inside my own .line-clamp class in a global style scss sheet: -webkit-line-clamp: var(--lines); and then in my html: [class]="maxLines > 0 ? ' line-clamp' : ''" and [style]="'--lines: ' + maxLines" That works to dynamically adjust the clamping behavior. I assume it re-evaluates the --lines var in the scss every time it re-renders, but tbh I'm not 100% sure how it works. Is something like that possible here?
Ah ok - that helps me (basically moving it out of CSS into a typescript service I can call on init of my app component to add the fonts). If you can add that to your answer, that'd be great. I'll accept your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.