JSPM

@digitaliseringsbyran/tailwindcss-screens-in-dom

0.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 9
  • Score
    100M100P100Q51352F
  • License ISC

Expose the active tailwind screen to the DOM.

Package Exports

  • @digitaliseringsbyran/tailwindcss-screens-in-dom

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@digitaliseringsbyran/tailwindcss-screens-in-dom) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

tailwindcss-screens-in-dom

Expose the active tailwind screen (like sm, md) in body:before, allowing you to use the value in Javascript. Read more

Installation

$ npm install tailwindcss-screens-in-dom --save-dev

Usage

Add the plugin to the plugins array in your tailwind config.

plugins: [
  // ...
  require('./tailwindcss-screens-in-dom')()
]

Options

You can pass an object to override the default settings.

// Default options

plugins: [
  require('./tailwindcss-screens-in-dom')({
    noScreen: 'xs' // Viewports below the smallest defined screen.
  })
]

Access the active screen in Javascript

Accessing the value in plain Javascript is explained here.

React

A example hook that returns the active screen as a string:

useTailwindScreen.js

import { useState, useEffect } from 'react'

export default () => {
  const isClient = typeof window === 'object'

  function getScreen() {
    return window
      .getComputedStyle(document.querySelector('body'), ':before')
      .getPropertyValue('content')
      .replace(/"|'/g, '')
  }

  const initialState = isClient ? getScreen() : ''
  const [screen, setScreen] = useState(initialState)

  useEffect(() => {
    if (!isClient) {
      return false
    }

    function handleResize() {
      setScreen(getScreen())
    }

    window.addEventListener('resize', handleResize)
    return () => window.removeEventListener('resize', handleResize)
  }, [])

  return screen
}

Component

import React from 'react'
import useTailwindBreakpoint from '../hooks/useTailwindBreakpoint'

const ActiveScreen => {
  const screen = useTailwindBreakpoint()
  
  return (
      <span>{screen}</span>
  )
}

export default ActiveScreen