Git Product home page Git Product logo

508_accessibility_visual_focus_example's Introduction

On form pages for the Ruby on Rails framework, a new element, <div id='error-explanation'> will appear when a form is submitted and an error is thrown. In order to be compliant to a user using assistive technology, the visual focus must move to the revealed content.

This can be achieved by using javascript to move the focus with the focus element and setting the tabindex attribute to tabindex=0 on the error-explanation element.

By default, the color contrast of the standard Rails scaffold error-explanation (see the _form.html.erb view for your model) div is usually not 508 compliant. Here's an example of how the styling can be changed to fix it:

Note the new ID from error-explanation to error_explanation

HTML at app/views/model_name/_form.html.erb:

  <% if @model_name.errors.any? %>
    <div id="error_explanation" tabindex="0">
      <h2><%= pluralize(@model_name.errors.count, "error") %> prohibited this post from being saved:</h2>
      <ul>
        <% @model_name.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

CSS:

.field_with_errors {
  padding: 2px;
  display: table;
}

#error_explanation {
  width: 450px;
  border: 2px solid red;
  padding: 7px;
  padding-bottom: 0;
  margin-bottom: 20px;
  background-color: #f0f0f0;

Now that the color and markup is taken care of, here's one implementation of how to go about moving the visual focus this in vanilla Javascript:

Javascript:

$( document ).ready(function() {
  seterrorFocus();
});

function setErrorFocus() {
  var error_explanation = document.getElementById('error_explanation');
  if (error_explanation != null) {
    error_explanation.focus();
    // There's nothing special about the 'focus' class. It's just a class
    // To test for executing the above code to add the focus for the feature test
    error_explanation.classList.add('focus');
  };
}

Feature test at spec/model_name/model_name_create_spec.rb:

RSpec.describe 'model_name creation page', js: true do
  context 'Accessibility Related' do
    it 'should bring visual focus to first missing field element', js: true do
        click_button 'Create Model_name'
        // Choose whatever the error page brings to
        expect(page.current_path).to eq model_name_page_path
        // Can check for the full element to assure the tabindex is 0
        expect(page).to have_selector(
          :css,
          "div[id='error_explanation']" \
          "[tabindex='0']"
        )
        // We added the class with javascript to assure that the full code executed
        page.find("#error_explanation")[:class].include?("focus")
      end
    end
  end
end

508_accessibility_visual_focus_example's People

Contributors

zasman avatar

Watchers

 avatar

Forkers

webaccesshelp

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.