Skip to content

Commit

Permalink
refactor: material symbols scss mixins + ts const (#45)
Browse files Browse the repository at this point in the history
* refactor: material symbols scss mixins + ts const

and some readonly here and there

* refactor: strip units if using px size
  • Loading branch information
davidlj95 authored Sep 30, 2023
1 parent 6865395 commit 3207f98
Show file tree
Hide file tree
Showing 18 changed files with 124 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<a
[href]="item.url"
[attr.aria-label]="item.name"
class="material-symbols-outlined"
[class]="MATERIAL_SYMBOLS_CLASS"
>{{ item.materialSymbol }}</a
>
</li>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@use 'breakpoints';
@use 'margins';
@use 'material-symbols';

:host {
ul {
Expand All @@ -15,13 +16,15 @@
li {
height: 32px;

.material-symbols-outlined {
font-variation-settings:
'FILL' 1,
'wght' 700,
'GRAD' 200,
'opsz' 32;
font-size: 32px;
@include material-symbols.class {
$fontSize: 32px;
font-size: $fontSize;
@include material-symbols.variation-settings(
$fill: true,
$weight: 700,
$grade: 200,
$size: $fontSize
);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Component } from '@angular/core'
import { Call, Email, MyLocation } from '../../material-symbols'
import { MATERIAL_SYMBOLS_CLASS } from '../../common/material-symbols'

@Component({
selector: 'app-contact-traditional-icons',
templateUrl: './contact-traditional-icons.component.html',
styleUrls: ['./contact-traditional-icons.component.scss'],
})
export class ContactTraditionalIconsComponent {
public items: ReadonlyArray<{
protected readonly MATERIAL_SYMBOLS_CLASS = MATERIAL_SYMBOLS_CLASS
public readonly items: ReadonlyArray<{
name: string
materialSymbol: string
url: URL
Expand Down
3 changes: 2 additions & 1 deletion src/app/about/description/description.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
</ng-template>
<ng-template #dataTemplate>
<span
class="symbol material-symbols-outlined"
[class]="MATERIAL_SYMBOLS_CLASS"
class="symbol"
aria-hidden="true"
*ngIf="line.data"
>{{ line.data.symbol }}</span
Expand Down
14 changes: 8 additions & 6 deletions src/app/about/description/description.component.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@use 'margins';
@use 'material-symbols';
@use 'paddings';

:host {
Expand Down Expand Up @@ -37,13 +38,14 @@
}
}

.material-symbols-outlined {
@include material-symbols.class {
font-size: 1em;
font-variation-settings:
'FILL' 1,
'wght' 700,
'GRAD' 200,
'opsz' 24;
@include material-symbols.variation-settings(
$fill: true,
$weight: 700,
$grade: 200,
$size: 24
);
}

button {
Expand Down
17 changes: 10 additions & 7 deletions src/app/about/description/description.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
TIMING_FUNCTION,
} from '../../common/animations'
import { DescriptionLine } from '../../metadata'
import { MATERIAL_SYMBOLS_CLASS } from '../../common/material-symbols'

@Component({
selector: 'app-description',
Expand Down Expand Up @@ -60,22 +61,24 @@ export class DescriptionComponent {
@Input() protected parent?: DescriptionComponent

// 👇 Using `protected` to avoid being marked as unused
@HostBinding('class.displayBlockIfNoScript') protected visibleIfNoScript =
true
@HostBinding('class.displayBlockIfNoScript')
protected readonly visibleIfNoScript = true
@HostBinding('class.hidden') protected hidden = !this.isRenderingOnBrowser

private EXPANDED_DEFAULT_NO_JS = true
private EXPANDED_DEFAULT_JS_ENABLED = false
protected readonly MATERIAL_SYMBOLS_CLASS = MATERIAL_SYMBOLS_CLASS
private readonly EXPANDED_DEFAULT_NO_JS = true
private readonly EXPANDED_DEFAULT_JS_ENABLED = false
public isExpanded = this.isRenderingOnBrowser
? this.EXPANDED_DEFAULT_JS_ENABLED
: this.EXPANDED_DEFAULT_NO_JS
@ViewChildren(DescriptionComponent)
private children!: QueryList<DescriptionComponent>

constructor(
protected sanitizer: DomSanitizer,
@Inject(COLLAPSIBLE_CONFIG) protected config: CollapsibleConfiguration,
@Inject(PLATFORM_ID) private platformId: object,
protected readonly sanitizer: DomSanitizer,
@Inject(COLLAPSIBLE_CONFIG)
protected readonly config: CollapsibleConfiguration,
@Inject(PLATFORM_ID) private readonly platformId: object,
) {}

protected get isRenderingOnBrowser() {
Expand Down
2 changes: 2 additions & 0 deletions src/app/common/material-symbols.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Keep in sync with Material Symbols Sass
export const MATERIAL_SYMBOLS_CLASS = 'material-symbols-outlined'
4 changes: 2 additions & 2 deletions src/app/header/header.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
aria-label="Color scheme toggle (dark or light)"
id="dark-light-scheme-toggle"
>
<span class="material-symbols-outlined light-only">{{
<span [class]="MATERIAL_SYMBOLS_CLASS" class="light-only">{{
MaterialSymbol.DarkTheme
}}</span>
<span class="material-symbols-outlined dark-only">{{
<span [class]="MATERIAL_SYMBOLS_CLASS" class="dark-only">{{
MaterialSymbol.LightTheme
}}</span>
</button>
Expand Down
14 changes: 8 additions & 6 deletions src/app/header/header.component.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@use 'borders';
@use 'paddings';
@use 'header';
@use 'material-symbols';
@use 'z-index';
@use 'helpers/units';

Expand Down Expand Up @@ -28,13 +29,14 @@
font-size: header.$icons-height;
}

.material-symbols-outlined {
@include material-symbols.class {
font-size: 1em;
font-variation-settings:
'FILL' 0,
'wght' 200,
'GRAD' 0,
'opsz' #{units.stripUnit(header.$icons-height)};
@include material-symbols.variation-settings(
$fill: false,
$weight: 200,
$grade: 0,
$size: header.$icons-height
);
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/app/header/header.component.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { Component } from '@angular/core'
import { DarkTheme, LightTheme } from '../material-symbols'
import { ColorSchemeService } from './color-scheme.service'
import { MATERIAL_SYMBOLS_CLASS } from '../common/material-symbols'

@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.scss'],
})
export class HeaderComponent {
protected MaterialSymbol = {
protected readonly MATERIAL_SYMBOLS_CLASS = MATERIAL_SYMBOLS_CLASS
protected readonly MaterialSymbol = {
DarkTheme,
LightTheme,
}

constructor(protected colorSchemeService: ColorSchemeService) {}
constructor(protected readonly colorSchemeService: ColorSchemeService) {}
}
4 changes: 1 addition & 3 deletions src/app/no-script/no-script.component.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<noscript>
<div class="contents">
<p>
<span class="material-symbols-outlined">{{
MaterialSymbol.Warning
}}</span>
<span [class]="MATERIAL_SYMBOLS_CLASS">{{ MaterialSymbol.Warning }}</span>
<b>Seems you don't have JavaScript enabled</b>
</p>
<p>
Expand Down
14 changes: 8 additions & 6 deletions src/app/no-script/no-script.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
@use 'header';
@use 'paddings';
@use 'z-index';
@use 'material-symbols';

:host {
position: fixed;
Expand Down Expand Up @@ -32,13 +33,14 @@
font-size: 14px;
}

.material-symbols-outlined {
@include material-symbols.class {
font-size: 1em;
font-variation-settings:
'FILL' 0,
'wght' 700,
'GRAD' 0,
'opsz' 16;
@include material-symbols.variation-settings(
$fill: false,
$weight: 700,
$grade: 0,
$size: 16
);
}
}
}
4 changes: 3 additions & 1 deletion src/app/no-script/no-script.component.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Component } from '@angular/core'
import { Warning } from '../material-symbols'
import { MATERIAL_SYMBOLS_CLASS } from '../common/material-symbols'

@Component({
selector: 'app-no-script',
templateUrl: './no-script.component.html',
styleUrls: ['./no-script.component.scss'],
})
export class NoScriptComponent {
protected MaterialSymbol = {
protected readonly MATERIAL_SYMBOLS_CLASS = MATERIAL_SYMBOLS_CLASS
protected readonly MaterialSymbol = {
Warning,
}
}
58 changes: 58 additions & 0 deletions src/sass/_material-symbols.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
@use 'sass:math';
@use 'helpers/units';

@mixin font {
@font-face {
font-family: 'Material Symbols Outlined';
font-style: normal;
font-weight: 100 700;
font-display: block;
src:
url('../../assets/material-symbols-outlined-subset.woff2') format('woff2'),
url('../../assets/material-symbols-outlined-subset.woff') format('woff'),
url('../../assets/material-symbols-outlined-subset.ttf')
format('truetype');
}

@include class {
//noinspection CssNoGenericFontName
font-family: 'Material Symbols Outlined';
font-weight: normal;
font-style: normal;
font-size: 24px;
line-height: 1;
letter-spacing: normal;
text-transform: none;
display: inline-block;
white-space: nowrap;
word-wrap: normal;
direction: ltr;
-webkit-font-feature-settings: 'liga';
-webkit-font-smoothing: antialiased;
}

@include class {
@include variation-settings;
}
}

@mixin class {
.material-symbols-outlined {
@content;
}
}

@mixin variation-settings($fill: false, $weight: 400, $grade: 0, $size: 24) {
$sizeUnit: math.unit($size);
@if $sizeUnit == 'px' {
$size: units.strip($size);
} @else if $sizeUnit != '' {
@error "Size cannot have units other than 'px' (#{$size})";
}

font-variation-settings:
'FILL' if($fill, 1, 0),
'wght' $weight,
'GRAD' $grade,
'opsz' $size;
}
39 changes: 0 additions & 39 deletions src/sass/_typographies.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,3 @@
@mixin monospace-font {
font-family: 'Roboto Mono', 'Courier New', monospace;
}

@mixin material-symbols-font {
@font-face {
font-family: 'Material Symbols Outlined';
font-style: normal;
font-weight: 100 700;
font-display: block;
src:
url('../../assets/material-symbols-outlined-subset.woff2') format('woff2'),
url('../../assets/material-symbols-outlined-subset.woff') format('woff'),
url('../../assets/material-symbols-outlined-subset.ttf')
format('truetype');
}

.material-symbols-outlined {
//noinspection CssNoGenericFontName
font-family: 'Material Symbols Outlined';
font-weight: normal;
font-style: normal;
font-size: 24px;
line-height: 1;
letter-spacing: normal;
text-transform: none;
display: inline-block;
white-space: nowrap;
word-wrap: normal;
direction: ltr;
-webkit-font-feature-settings: 'liga';
-webkit-font-smoothing: antialiased;
}

.material-symbols-outlined {
font-variation-settings:
'FILL' 0,
'wght' 100,
'GRAD' 0,
'opsz' 24;
}
}
2 changes: 1 addition & 1 deletion src/sass/helpers/_units.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

// Strips unit from variable. ie: 32px -> 32
// https://stackoverflow.com/a/12335841/3263250
@function stripUnit($number) {
@function strip($number) {
@return math.div($number, ($number * 0+1));
}
3 changes: 2 additions & 1 deletion src/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
@use 'app/no-script/no-script-theme';
@use 'app/navigation-tabs/navigation-tabs-theme';
@use 'font-awesome';
@use 'material-symbols';
@use 'theming';
@use 'typographies';

Expand Down Expand Up @@ -38,7 +39,7 @@ pre {
border-width: 0;
}

@include typographies.material-symbols-font();
@include material-symbols.font();

@mixin app-color($theme) {
@include root-theme.color($theme);
Expand Down
2 changes: 1 addition & 1 deletion src/test/helpers/material-symbols.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { By } from '@angular/platform-browser'
import { MATERIAL_SYMBOLS_CLASS } from '../../app/common/material-symbols'

const MATERIAL_SYMBOLS_CLASS = 'material-symbols-outlined'
export const MATERIAL_SYMBOLS_SELECTOR = By.css(`.${MATERIAL_SYMBOLS_CLASS}`)

0 comments on commit 3207f98

Please sign in to comment.