Giter Site home page Giter Site logo

joshuatvernon / react-gracefully Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 2.0 628 KB

A library for gracefully rendering responsive React client or server side.

Home Page: https://www.npmjs.com/package/react-gracefully

License: MIT License

JavaScript 3.39% TypeScript 96.61%
breakpoints hook express-middleware react responsive gracefully server-side client-side ssr usegrace

react-gracefully's Introduction

react-gracefully logo

react-gracefully

npm install --save react-gracefully

🦢


Overview

A library for responsively and gracefully rendering client and server side React.

react-gracefully exposes Express Middleware and the GraceProvider React provider component that allows devices, breakpoints and window to be configured client and server side.

Additionally, react-gracefully exposes the useGrace React hook and Show and Hide React components to allow React code to be responsively and gracefully rendered client and server side.


Installation

react-gracefully can be installed with npm or yarn.

npm install --save react-gracefully

or

yarn add react-gracefully

Usage

useGrace hook

react-gracefully exposes the useGrace React hook to get access to devices, breakpoints and window details.

Devices

Here is an example of how to use the useGrace hook to access device details.

import React from 'react';
import { useGrace } from 'react-gracefully';

export const Page = () => {
  const { is } = useGrace();
  const isMobile = is.mobile();
  const isTablet = is.tablet();
  const isDesktop = is.tablet();
  const isAndroid = is.device('android');
  return (
    <div>
      {isAndroid && <h2>Android Title</h2>}
      {isMobile && <h2>Mobile Title</h2>}
      {(isTablet || isDesktop) && <h1>Tablet or Desktop Title</h1>}
    </div>
  );
};

Breakpoints

Here is an example of how to use the useGrace hook to access breakpoint details.

import React from 'react';
import { useGrace } from 'react-gracefully';

export const Page = () => {
  const { is } = useGrace();
  const isAboveSmall = is.above.breakpoint('sm');
  const isBelowLarge = is.below.breakpoint('lg');
  const isMedium = is.current.breakpoint('md');
  return <div>{isAboveSmall && isBelowLarge && isMedium && <h2>Medium Title</h2>}</div>;
};

Window

Here is an example of how to use the useGrace hook to access window details.

import React from 'react';
import { useGrace } from 'react-gracefully';

export const Page = () => {
  const { is } = useGrace();
  const isWindowHeightAbove2em = is.above.window.height('2em');
  const isWindowWidthBelow500px = is.below.window.width('500px');
  const isLandscape = is.current.window.landscape();
  return (
    <div>{isWindowHeightAbove2em && isWindowWidthBelow500px && isLandscape && <h2>Landscape Medium Title</h2>}</div>
  );
};

Components

react-gracefully exposes a number of components. These components can be used to setup the configuration or show or hide content server side.

Show component

The Show component can be used to show content for specific breakpoints, devices or window orientation. Since it uses media queries under the hood all content is returned from the server to the client and then hidden or shown using css. This means it can be used effectively in server side rendered apps such as Next.js.

import React from 'react';
import { Show } from 'react-gracefully';

export const Page = () => {
  return (
    <div>
      <Show devices={['mobile']}>
        <h2>Mobile Title</h2>
      </Show>
      <Show devices={['tablet', 'desktop']}>
        <h1>Tablet or Desktop Title</h1>
      </Show>
    </div>
  );
};

Hide component

The Hide component can be used to hide content for specific breakpoints, devices or window orientation. Since it uses media queries under the hood all content is returned from the server to the client and then hidden or shown using css. This means it can be used effectively in server side rendered apps such as Next.js.

import React from 'react';
import { Hide } from 'react-gracefully';

export const Page = () => {
  return (
    <div>
      <Hide devices={['mobile']}>
        <h1>Tablet or Desktop Title</h1>
      </Hide>
      <Hide devices={['tablet', 'desktop']}>
        <h2>Mobile Title</h2>
      </Hide>
    </div>
  );
};

GraceProvider component

The GraceProvider component is used to scope and configure use-gracefully for the app. It allows custom breakpoints and/or devices to be configured.

import React from 'react';
import { GraceProvider } from 'react-gracefully';

export const App = () => {
  const breakpoints: Breakpoints = {
    sm: {
      max: '500px'
    },
    md: {
      min: '500px',
      max: '1000px'
    },
    lg: {
      min: '1000px'
    }
  };
  const devices = ['mobile', 'ios', 'android', 'tablet', 'desktop'];
  return (
    <GraceProvider breakpoints={breakpoints} devices={devices}>
      <Router />
    </GraceProvider>
  );
};

Express Middleware

Default (UserAgent)

The react-gracefully express middleware by default will use the user-agent to sniff the current device type.

import express from 'express';
import grace from 'react-gracefully';

const app = express();
app.use(grace.express());

Custom (Headers)

Optionally, react-gracefully express middleware can be configured to check custom headers for the current device type.

import express from 'express';
import grace, { Config, Headers } from 'react-gracefully';

const app = express();
const config: Config = {
  devices: {
    mobile: (headers: Headers) => headers['x-device-type'] === 'mobile',
    tablet: (headers: Headers) => headers['x-device-type'] === 'tablet',
    desktop: (headers: Headers) => headers['x-device-type'] === 'desktop',
    ios: (headers: Headers) => headers['x-device-type'] === 'ios',
    android: (headers: Headers) => headers['x-device-type'] === 'android'
  }
};
app.use(grace.express(config));

Copyright 🦢

MIT

react-gracefully's People

Contributors

dependabot[bot] avatar joshuatvernon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

react-gracefully's Issues

Create tests

  • Get test coverage to >90%
  • Add enzyme mounted tests (+ snapshot tests) for Show and Hide

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.