No results found

Tradeoffs in Component Design

HTML Streaming Order Challenges

When building accessible components in Qwik, we face unique challenges due to how HTML is streamed from the server to the client. These challenges particularly affect components with related elements like form controls, labels, and descriptions.

The Connection Problem

In standard HTML, elements are often connected through attributes:

These connections create accessibility relationships that screen readers and other assistive technologies rely on.

The Challenge with Streaming Rendering

Why Traditional Nesting Doesn't Always Work

Consider this common component pattern:

<Field.Root>
  <Field.Label>Email</Field.Label>
  <Field.Input />
  <Field.Description>Enter your work email</Field.Description>
  <Field.Error>Invalid email Fieldat</Field.Error>
</Field.Root>

In a streaming environment, components render sequentially. By the time Field.Description is encountered, Field.Input has already been sent to the browser. This creates two significant issues:

  1. Missing References: We can't retroactively add the description's ID to the input's aria-describedby attribute
  2. Unpredictable Component Order: We can't assume users will always place components in a specific order

Example: Checkbox with Description

By checking this box, you acknowledge that you have read, understood, and agree to our Terms of Service and Privacy Policy. This includes consent to process your personal data as described in our policies.

In this example, the Root component provides context for the Trigger and Label. However, when a user adds a Description component:

<Checkbox.Root>
  <Checkbox.Trigger />
  <Checkbox.Label>Accept terms</Checkbox.Label>
  <Checkbox.Description>Read our terms and conditions</Checkbox.Description>
</Checkbox.Root>

The Trigger has already been streamed before we discover the Description exists!

Future Improvements

Some potential solutions:

1. Out-of-Order Streaming

Qwik could support mechanisms to revisit and update already-streamed HTML, allowing components to communicate after initial rendering.

This has been proposed in a Qwik RFC and would enable more intuitive component APIs.

2. Deferred Rendering

Components could defer rendering critical attributes until all children have been analyzed, then "catch up" with the necessary connections.

Contribute to This Challenge

This remains an active area of research in Qwik's component model. We welcome contributions and ideas for solving this streaming composition problem!

If you have approaches that might help balance accessibility needs with intuitive APIs, please share them with the community.