How to Build an Accessible React Loading Overlay Component

Build an accessible React loading overlay with reusable TypeScript, CSS, async state handling, fullscreen support, and practical UX guidance.

ASOasis
6 min read
How to Build an Accessible React Loading Overlay Component

Image used for representation purposes only.

A loading overlay gives people clear feedback while an operation is in progress and, when needed, prevents them from interacting with the content underneath. A good React loading overlay is small, reusable, accessible, and driven by the real state of the work—not by a timer.

This guide builds a dependency-free overlay that can cover a panel or the entire viewport, explains how to wire it to asynchronous work, and covers accessibility and common implementation mistakes.

What a loading overlay should do

A loading overlay typically combines a visual indicator with a short status message. Depending on its scope, it may also block pointer interaction with the content it covers. It should:

  • Appear only while relevant work is actually in progress.
  • Make its message available to assistive technology.
  • Clearly communicate whether it covers a component or the whole page.
  • Remain usable on narrow screens and with reduced-motion settings.
  • Avoid trapping focus or claiming to be a modal unless it implements modal behavior.

React supports rendering UI conditionally with ordinary JavaScript, so the component can simply return null when it is not visible. (react.dev )

Build a reusable React component

The following TypeScript component supports both a locally positioned overlay and a viewport-wide overlay. It uses a status role to announce the message, while hiding the decorative spinner from screen readers.

import type { CSSProperties } from 'react';

type LoadingOverlayProps = {
  visible: boolean;
  message?: string;
  fullscreen?: boolean;
};

export function LoadingOverlay({
  visible,
  message = 'Loading…',
  fullscreen = false,
}: LoadingOverlayProps) {
  if (!visible) return null;

  return (
    <div
      className={`loading-overlay${fullscreen ? ' loading-overlay--fullscreen' : ''}`}
      role="status"
      aria-live="polite"
      aria-label={message}
    >
      <span className="loading-overlay__spinner" aria-hidden="true" />
      <span>{message}</span>
    </div>
  );
}

The CSSProperties import is not required for this version and can be omitted; it is included only if you later add typed inline styles. The example below works without it.

.loading-overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  padding: 1rem;
  color: #172033;
  background: rgb(255 255 255 / 82%);
  cursor: wait;
}

.loading-overlay--fullscreen {
  position: fixed;
  z-index: 1000;
  background: rgb(15 23 42 / 55%);
  color: white;
}

.loading-overlay__spinner {
  width: 1.5rem;
  height: 1.5rem;
  flex: 0 0 auto;
  border: 3px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: loading-overlay-spin 0.8s linear infinite;
}

@keyframes loading-overlay-spin {
  to { transform: rotate(360deg); }
}

@media (prefers-reduced-motion: reduce) {
  .loading-overlay__spinner {
    animation: none;
  }
}

Remove the unused import from the component if you copy the code as written. The overlay uses position: absolute by default, so its nearest positioned ancestor defines its bounds. Give that ancestor position: relative.

Use it over a section

Keep the overlay and the content it covers inside a positioned wrapper. Mark the region as busy while its content is being updated.

function ResultsPanel({ loading }: { loading: boolean }) {
  return (
    <section className="results-panel" aria-busy={loading}>
      <LoadingOverlay visible={loading} message="Loading results…" />
      <h2>Search results</h2>
      {/* Results or an empty state go here. */}
    </section>
  );
}
.results-panel {
  position: relative;
  min-height: 12rem;
}

The overlay covers the panel, and its default background makes the underlying content less prominent. If the content should remain readable during loading, use a more transparent background or choose a small inline indicator instead. A loading overlay is not automatically a dialog: this implementation does not move focus, trap keyboard navigation, or provide a close action.

Connect visibility to asynchronous work

The component should reflect operation state. Set it before starting the request and clear it in finally, so both success and failure end the loading state.

import { useState } from 'react';

function RefreshButton() {
  const [loading, setLoading] = useState(false);

  async function refresh() {
    setLoading(true);
    try {
      await fetch('/api/results');
      // Parse the response and update application data here.
    } catch (error) {
      console.error('Could not refresh results', error);
      // Show an error message in the UI.
    } finally {
      setLoading(false);
    }
  }

  return (
    <div className="results-panel" aria-busy={loading}>
      <LoadingOverlay visible={loading} message="Refreshing results…" />
      <button type="button" onClick={refresh} disabled={loading}>
        {loading ? 'Refreshing…' : 'Refresh results'}
      </button>
    </div>
  );
}

In production, also check the response status and handle cancellation or overlapping requests if the component can start more than one operation. Avoid setting loading state on a fixed timeout: network and server latency vary, and a timer can hide the indicator too early or keep it visible after work has finished.

Choose the right overlay scope

For a section or card, prefer the default absolute overlay. It stays associated with the area that is loading and avoids blocking unrelated controls. Use fullscreen for a genuinely app-wide operation, such as an initial route transition or a workflow that should prevent interaction across the page.

If a fullscreen overlay is clipped, check for ancestor elements that establish stacking contexts or contain fixed-position descendants. For more complex layouts, a React portal can render the overlay into a different DOM location, such as document.body; a portal changes physical DOM placement while preserving its relationship to the React tree. (react.dev ) For server-rendered applications, make sure any portal target is accessed only where the browser DOM exists.

Accessibility and UX considerations

  • Use a concise, specific message such as “Saving changes…” rather than a vague “Please wait.”
  • Set aria-busy on the region whose content is being updated. Keep the live status announcement focused on the loading message.
  • Keep the spinner decorative with aria-hidden="true"; the text provides the useful status.
  • Do not use aria-modal="true" on a basic overlay. A modal needs appropriate focus management and keyboard behavior; visual coverage alone does not provide those features.
  • Consider disabling the control that started the operation to reduce accidental duplicate submissions. Make sure failures are communicated and the control becomes available again.
  • Respect reduced-motion preferences, as in the CSS example. If animation is removed, keep the text or another clear visual cue.
  • For very short operations, avoid adding arbitrary delays just to show the overlay. If brief flashes are a real UX problem, consider a deliberate, tested display policy rather than misrepresenting the operation’s state.

When to use Suspense instead

A manually controlled overlay is useful for events such as submitting a form, refreshing data, or saving a record because the event handler owns the operation state. For code or data that suspends, React’s <Suspense> provides a separate fallback mechanism: React can show the fallback until the relevant suspended content is ready. (react.dev ) These approaches solve related but distinct problems, so choose based on how the application’s data and rendering are managed.

Common mistakes to avoid

  1. Forgetting the positioned parent. An absolutely positioned overlay may cover the wrong area unless its intended wrapper establishes a positioning context.
  2. Leaving loading stuck after an error. Put the state reset in finally and present an error state separately.
  3. Using the overlay for every request. Local indicators often provide better context and leave unrelated parts of the page usable.
  4. Calling it a modal without modal behavior. Do not promise a keyboard or focus experience the component does not implement.
  5. Treating the spinner as the status. A spinner alone does not explain what is happening; include an accessible, meaningful message.

Conclusion

A reliable React loading overlay can be built with a small conditional component, a positioned wrapper, and CSS. Keep visibility tied to the actual operation, scope the overlay to the smallest relevant region, and use accessible status text. For whole-page blocking behavior or complex dialogs, add the necessary focus and keyboard management rather than relying on the visual layer alone.

Related Posts