Skip to content

Commit dbc3d8e

Browse files
arraypressclaude
andcommitted
A hidden label does not take the tooltip with it
`hide_label` was folded into `$with_label`, which meant something else: that the caller is drawing its own heading somewhere, and so owns the badge and the tooltip that would sit beside it. Under `hide_label` nobody draws a heading, so suppressing the tooltip sent it nowhere at all -- which is how a long description moved into a tooltip and then vanished. The two are separate now. The legend or label is hidden and still announced; the tooltip stays where the control is. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tk8y5eZa3CHVVSM6nVxsJw
1 parent 49326a6 commit dbc3d8e

1 file changed

Lines changed: 36 additions & 10 deletions

File tree

‎src/Renderer.php‎

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,6 @@ final class Renderer {
6868
* @return string
6969
*/
7070
public function render( Field $field, string $error = '', bool $with_label = true ): string {
71-
// A field whose label is redundant beside what surrounds it -- a
72-
// billing chooser under a heading that already says Pricing -- can
73-
// ask for the label to be kept for assistive technology and not
74-
// drawn. Dropping the label instead leaves a group of radios with no
75-
// name at all, which is a different thing from one whose name is
76-
// obvious on screen.
77-
if ( $field->get( 'hide_label', false ) ) {
78-
$with_label = false;
79-
}
8071

8172
$type = $field->type();
8273

@@ -250,6 +241,27 @@ private function wrap_labelled( Field $field, string $control, string $describer
250241
// beside the control.
251242
$tooltip = $with_label ? Tooltip::for_field( $field ) : '';
252243

244+
// A label the caller wants kept for assistive technology and not
245+
// drawn. Unlike $with_label, which means somebody else is drawing the
246+
// heading and owns the badge and tooltip with it, this leaves the
247+
// tooltip where it is -- there is no other heading for it to sit
248+
// beside.
249+
if ( $field->get( 'hide_label', false ) ) {
250+
$badge = '';
251+
252+
return $this->wrapper(
253+
$field,
254+
sprintf(
255+
'<label class="screen-reader-text" for="%s">%s</label>%s%s%s',
256+
esc_attr( $field->input_id() ),
257+
esc_html( $field->label() ),
258+
$tooltip,
259+
$control,
260+
$describers
261+
)
262+
);
263+
}
264+
253265
// A self-labelling control already carries its text; a second label
254266
// above it would announce the field twice.
255267
if ( $with_label && ! $field->type()->is_self_labelling() && '' !== $field->label() ) {
@@ -302,14 +314,28 @@ private function wrap_fieldset( Field $field, string $control, string $describer
302314
$tooltip = $with_label ? Tooltip::for_field( $field ) : '';
303315
$in_legend = $badge . $tooltip;
304316

317+
// A field whose name is obvious from what surrounds it -- a billing
318+
// chooser on the Pricing tab -- can keep its legend for assistive
319+
// technology without drawing it. That is not the same as a caller
320+
// drawing its own heading, which is what $with_label means and why
321+
// that one takes the badge and tooltip with it: here nobody draws a
322+
// heading, so the tooltip has to stay or it goes nowhere at all.
323+
$show_label = $with_label && ! $field->get( 'hide_label', false );
324+
325+
if ( ! $show_label ) {
326+
$badge = '';
327+
$tooltip = $with_label ? Tooltip::for_field( $field ) : '';
328+
$in_legend = '';
329+
}
330+
305331
$legend = '' === $field->label()
306332
? ''
307333
: sprintf(
308334
// The inner span is core's own shape — options-general.php and
309335
// options-discussion.php both write it this way — and exists
310336
// because a legend cannot be positioned reliably on its own.
311337
'<legend class="field-kit__legend%s"><span>%s%s%s</span></legend>',
312-
$with_label ? '' : ' screen-reader-text',
338+
$show_label ? '' : ' screen-reader-text',
313339
esc_html( $field->label() ),
314340
$this->required_marker( $field ),
315341
$in_legend

0 commit comments

Comments
 (0)