Atomic Layout
  • Introduction
  • Motivation
  • Resources
  • FAQ
    • Comparison with styled-system
  • Getting started
    • Installation
    • First composition
    • Responsive composition
    • Nested composition
  • Fundamentals
    • Breakpoints
    • Prop aliases
    • Responsive props
  • API
    • Layout
      • configure()
    • Components
      • Box
      • Composition
      • Only
      • Visible
    • Hooks
      • useMediaQuery
      • useResponsiveQuery
      • useViewportChange
      • useBreakpointChange
      • useResponsiveValue
      • useResponsiveProps
    • Utilities
      • query
      • makeResponsive
  • Recipes
    • Semantics
    • Namespaces
    • Iterative areas
    • Responsive layout
    • Explicit media query
Powered by GitBook
On this page
  • Specification
  • Definition
  • Example
  1. API
  2. Hooks

useResponsiveValue

Specification

Returns a value based on the given breakpoint-value pairs. Whenever viewport matches a given breakpoint, its associated value is returned. Returns the default value, if any, when no breakpoints are matched.

Definition

type useResponsiveValue<T> = (
  breakpoints: Record<string, T>,
  defaultValue?: T,
)

Example

import React from 'react'
import { useResponsiveValue } from 'atomic-layout'

export const ReadTime = ({ duration }) => {
  // Truncate a label caption based on the viewport
  const caption = useResponsiveValue(
    {
      // Use contraction on extra-small devices
      xs: 'min.',
    },
    // On all other breakpoints use the full label
    'minutes(s)'
  )
  
  return <p>Reading duration: {duration} {caption}</p>
})
PrevioususeBreakpointChangeNextuseResponsiveProps

Last updated 5 years ago