Skip to content
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

@font-face in global.scss is not recognized when shadow is true #1875

Open
lucascco opened this issue Sep 19, 2019 · 1 comment
Open

@font-face in global.scss is not recognized when shadow is true #1875

lucascco opened this issue Sep 19, 2019 · 1 comment
Labels

Comments

@lucascco
Copy link

Stencil version:

 @stencil/core@1.4.0 

I'm submitting a:
[x ] bug report
[ ] feature request
[ ] support request => Please do not submit support requests here, use one of these channels: https://stencil-worldwide.herokuapp.com/ or https://forum.ionicframework.com/

Current behavior:
When I set the shadow as true, my @font-face added in global.scss is not recognized in my-component.scss

Expected behavior:
The @font-face should be recognized when used in attr css font-family.

Steps to reproduce:

Related code:

stencil.config.ts

export const config: Config = {
  plugins: [
    sass({
      injectGlobalPaths:[
        'src/global/style/global.scss',
      ],
    }),
  ],
}

global/style/global.scss

@font-face {
  font-family: LatoTest;
  src: url(assets/fonts/Lato-Regular.woff2) format('woff2'),
       url(assets/fonts/Lato-Regular.woff) format('woff');
}

@font-face {
  font-family: MontserratTest;
  src: url(assets/fonts/Montserrat_regular.ttf)  format('truetype');
}

my-component/my-component.scss

.nav-container {
  width: 100%;
  background: white;
  position: fixed;
  left: 0;
  top: 0;
  font-family: 'MontserratTest';
}

my-component/my-component.tsx

@Component({
  tag: 'my-component',
  styleUrl: 'my-component.scss',
  shadow: false,
})
export class MyComponent {
  render () {
    return (
      <div class="nav-container">
          My component
      </div>
    );
  }
}

Other information:
The global style is added inside the shadow but don't working.

@ionitron-bot ionitron-bot bot added the triage label Sep 19, 2019
@lucascco lucascco changed the title @font-face ing global.scss is not recognized when shadow is true @font-face in global.scss is not recognized when shadow is true Sep 19, 2019
@Nightmaster
Copy link
Contributor

Nightmaster commented Mar 23, 2020

@font-face is not working properly in some browsers when it's declared in a shadowDOM. The workaround is to create a style element with JS and to append it to your head:

@Component({
  tag: 'my-component',
  styleUrl: 'my-component.scss',
  shadow: false,
})
export class MyComponent {
  componentWillLoad() {
    const fontDeclarationElement: HTMLStyleElement = document.createElement('style');

  fontDeclarationElement.textContent +=
    '@font-face{font-family:LatoTest;src:url(assets/fonts/Lato-Regular.woff2) format("woff2"),url(assets/fonts/Lato-Regular.woff) format("woff");}'
    + '@font-face{font-family:MontserratTest;src:url(assets/fonts/Montserrat_regular.ttf)  format("truetype");}';
    document.head.append(fontDeclarationElement)
  }

  render () {
    return (
      <div class="nav-container">
          My component
      </div>
    );
  }
}

This should clear your problem.

N.B.: Personally I add an id to the style element to be sure to not create it multiple times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants