Skip to content

imhalawa/TS-MediaQueries-Handler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Typescript MediaQueries Handler

a simple hack that provide an easy way to detect screen sizes {xsmall, small, medium, large, xlarge} and apply actions.

Usage:

  1. Include the injector.css into your project [can be found in the styles directory]

    <link rel="stylesheet" href="css/injector.css">
  2. Import the MediaQueriesHandler.ts file in your typescript.

     import { MediaQueryHandler } from "./Helpers/MediaQueriesHandler";
  3. Extend MediaQueriesHandler class and Implement the following functions

    • OnXSmallScreens()

    • OnSmallScreens()

    • OnMediumScreens()

    • OnLargeScreens()

    • OnXLargeScreens()

Example

  • index.html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Breakpoints</title>
    <link rel="stylesheet" href='styles/Injector.css'>
</head>

<body>

    <h1 id="screenSize"></h1>

    <!-- script -->
    <script src='scripts/app.js' defer></script>
</body>

</html>
  • app.ts
import { MediaQueryHandler } from "./Helpers/MediaQueriesHandler";

class app extends MediaQueryHandler {
  constructor() {
    super();
    this.heading = document.querySelector("#screenSize")! as HTMLHeadingElement;
  }

  private heading: HTMLHeadingElement;

  OnXSmallScreens(): void {
    console.log("Xsmall  Screens");
    this.heading.textContent = "Xsmall  Screens";
  }
  OnSmallScreens(): void {
    console.log("Small  Screens");
    this.heading.textContent = "Small  Screens";
  }
  OnMediumScreens(): void {
    console.log("Medium  Screens");
    this.heading.textContent = "Medium  Screens";
  }
  OnLargeScreens(): void {
    console.log("Large  Screens");
    this.heading.textContent = "Large  Screens";
  }

  OnXLargeScreens(): void {
    console.log("XLarage  Screens");
    this.heading.textContent = "XLarage Screens";
  }
}

new app();

Note

Incase you're not aware of bundlers, you will need to bundle the file that extends the MediaQueryHandler class using either Webpack or Browersft to bundle all your files into one.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published