12.0.0 • Published 3 years ago

@anexia/platform-tools v12.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Platform Tools

Provides Ionic Framework way class mapping of current platform and orientation on the root document node ´html´ for usage inside scss or :host-context selecting.

Usage

Simply inject the service in your root app component and it will add classes to your document.

import {PlatformToolsService} from "./platform-tools.service";

export class AppComponent {
  public constructor(
      // ... 
    private readonly platformService: PlatformToolsService
      // ... 
  ) {

  }
}

Now you're able to use :host-context(.plt-tablet) at Angular Component SCSS level for writing context based css, or use at global.scss/style.scss as selector .plt-tablet > app-component { ... }.

Available class selectors:

Selector NameDescription
plt-androida device running Android
plt-desktopa desktop device
plt-iosa device running iOS
plt-ipadan iPad device
plt-iphonean iPhone device
plt-mobilea mobile device
plt-mobileweba web browser running in a mobile device
plt-phableta phablet device
plt-pwaa PWA app
plt-tableta tablet device
is-landscapea device with landscape orientation
is-portraita device with portrait orientation

Example

Inside your Angular Component Stylesheet SCSS

:host {
  display: block;
  width: 100%;
  height: 100vh;
}

// set color of font to white on ios platform
:host-context(.plt-ios) {
  :host {
    color: white;
  }
}
// set background color to blue on tablet
:host-context(.plt-tablet) {
  :host {
    background-color: blue;
  }
  
  // set background color red if tablet and landscape
  :host-context(.is-landscape) {
    :host {
      background-color: red;
    }
  }
}

Or inside your style.scss / global.scss without :host-context since it is only available inside component scss scopes.

html.plt-ios {
 // ... your rules
}

// or
.plt-tablet{

}

Notes

Platform class mapping won't change if simply changing device emulator inside Chrome since platform evaluation is done only on boot. A Reload will be needed. Screen orientation will be update on screenorientation change by listening to the window resize event.

Author

Credits

To the Ionic Framework core team for the inspiration.