label = $config['labelElement'] ?? new Tag( 'span' ); // Initialization $this->label->addClasses( [ 'oo-ui-labelElement-label' ] ); $this->setLabel( $config['label'] ?? null ); $this->setInvisibleLabel( $config['invisibleLabel'] ?? false ); $this->registerConfigCallback( function ( &$config ) { if ( $this->labelValue !== null ) { $config['label'] = $this->labelValue; } if ( $this->invisibleLabel !== false ) { $config['invisibleLabel'] = $this->invisibleLabel; } } ); } /** * Set the label. * * An empty string will result in the label being hidden. A string containing only whitespace will * be converted to a single ` `. * * @param string|HtmlSnippet|null $label Label text * @return $this */ public function setLabel( $label ) { $this->labelValue = (string)$label !== '' ? $label : null; $this->label->clearContent(); if ( $this->labelValue !== null ) { if ( is_string( $this->labelValue ) && trim( $this->labelValue ) === '' ) { $this->label->appendContent( new HtmlSnippet( ' ' ) ); } else { $this->label->appendContent( $label ); } } $visibleLabel = $this->labelValue !== null && !$this->invisibleLabel; $this->toggleClasses( [ 'oo-ui-labelElement' ], $visibleLabel ); return $this; } /** * Set whether the label should be visually hidden (but still accessible to screen-readers). * * An empty string will result in the label being hidden. A string containing only whitespace will * be converted to a single ` `. * * @param bool $invisibleLabel * @return $this */ public function setInvisibleLabel( $invisibleLabel ) { $this->invisibleLabel = (bool)$invisibleLabel; $this->label->toggleClasses( [ 'oo-ui-labelElement-invisible' ], $this->invisibleLabel ); // Pretend that there is no label, a lot of CSS has been written with this assumption $visibleLabel = $this->labelValue !== null && !$this->invisibleLabel; $this->toggleClasses( [ 'oo-ui-labelElement' ], $visibleLabel ); return $this; } /** * Get the label. * * @return string|HtmlSnippet|null Label text */ public function getLabel() { return $this->labelValue; } }