GithubHelp home page GithubHelp logo

react-announce-doc-title's Introduction

WIP: React Announceable Document Title

Provides a declarative way to specify document.title in a single-page app that will be announced to screen readers when it changes. This component can be used on server side as well.

Built with React Side Effect, built off of React Document Title and inspired by a11y-announcer for Ember.js

NOTE: This is a work in progress!!

Installation

npm install --save jasonblanchard/react-announceable-document-title

Dependencies: React >= 0.13.0

Features

  • Like a normal React compoment, can use its parent's props and state;
  • Can be defined in many places throughout the application;
  • Supports arbitrary levels of nesting, so you can define app-wide and page-specific titles;
  • Works with universal apps.
  • Announces page title changes to screen readers

Usage

1. Set the title

Wrap page containers in the <AnnounceDocTitle> component:

import React from 'react';
import { AnnounceDocTitle } from 'react-announce-doc-title';

export default class AboutPage extends React.Component {
  render() {
    return(
      <AnnounceDocTitle title="About - React App">
        <div>
          All about this sweet, sweet single page app.
        </div>
      </AnnounceDocTitle>
    );
  }

If you have <AnnounceDocTitle> components nested inside other <AnnounceDocTitle> components, the inner-most title will win.

2. Add the announcer component

In your root container or main page layout, add the <A11yToolkitAnnouncer /> container to hold the announce messages:

import React from 'react';
import { A11yToolkitAnnouncer } from 'react-announce-doc-title';

export default class RootContainer extends React.Component {
  render() {
    return (
      <div>
        <A11yToolkitAnnouncer />
        
        <h1>Single Page React App</h1>
        {this.props.children}
      </div>
    );
  }

This will add a visibly hidden (but reachable by screen readers) element on the page that has an aria-live tag that will automatically announce when the content changes. Each time the page title updates, it will populate here and trigger an announcement.

It's best practice that this container is available on the page at page load, not added later via a JS script. Using the <A11yToolkitAnnouncer /> in your root continer assumes you are rendering your app to string on the server as a universal (or isomorpic) app. (See below for more details on server rendering the page title). If you are not building a universal app, do not use the <A11yToolkitAnnouncer /> and add the announcer div manually on your HTML page following these instructions from a11y-toolkit.

3. Get page title on the server

When rendering your app on the server, you'll want to pull out the page title from your component tree and set it in the template so that it has the right title on initial pagel load. Here's a truncated example using Express:

server.js:

import express from 'express';
import exphbs from 'express-handlebars';
import React from 'react';
import { AnnounceDocTitle } from 'react-announce-doc-title';
import RootContainer from '../shared/containers/RootContainer';

const app = express();

app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');

...

app.get('/*', (req, res) => {
  const markup = React.renderToString(<RootContainer />);
  const documentTitle = AnnounceDocTitle.rewind();
  
  res.render('index', {
    markup: markup,
    documentTitle: documentTitle
  });
});

index.handlebars:

<!DOCTYPE html>
<html>
  <head>
    <title>{{documentTitle}}</title>
  </head>
  <body>
    <div id='app'>{{{markup}}}</div>
  </body>
</html>

Because this component keeps track of mounted instances, you have to make sure to call rewind on server after renderToString, or you'll get a memory leak. More info in the React Side Effect docs.

react-announce-doc-title's People

Contributors

jasonblanchard avatar

Stargazers

Michael Ball avatar Mathieu M-Gosselin avatar

Watchers

James Cloos avatar  avatar

Forkers

marcysutton

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.