Button
A button component that can be rendered as another tag or focusable when disabled.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Button;
#[Component]
function ButtonHeroDemo(): Node
{
return <Button>Submit</Button>;
}
Examples#
Rendering as another tag#
The button can remain keyboard accessible while being rendered as another tag, such as a <div>, by specifying tag="div".
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Button;
#[Component]
function ButtonAnotherTagDemo(): Node
{
return <Button tag="div">Button that can contain complex children</Button>;
}
Loading states#
For buttons that enter a loading state after activation, specify focusableWhenDisabled so focus remains on the button while it is disabled. Because some browser and screen reader combinations do not reliably announce changes to a focused button’s descendant text, use aria-labelledby to make the changing text the button’s explicit accessible name.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Button;
#[Component]
final class ButtonLoadingDemo
{
public function submit(): void
{
// Simulate a request that ends after 4 seconds.
sleep(4);
}
public function render(): Node
{
return (
<Button focusableWhenDisabled aria-labelledby="loading-label" onClick={{ submit }} phem:lock>
<span id="loading-label">
<span className="in-aria-disabled:hidden">Submit</span>
<span className="not-in-aria-disabled:hidden">Submitting</span>
</span>
</Button>
);
}
}
API reference#
Import the components of the family:
use function Phem\Ui\Button;Button#
A button: Base UI Button.
With focusableWhenDisabled, a disabled button keeps the focus (for a loading state): it gets aria-disabled in place of disabled. A button with aria-disabled="true" has the disabled look, also when phem:lock locks it while its event runs.
A button with another tag (tag="div"), and a button with focusableWhenDisabled, use the phem-button controller: Enter and Space click the button, and a disabled button does nothing.
The triggers and the close buttons of the other components render this Button, so a replacement of it (#[Component(replaces: Button::class)]) changes them too.
Do not render a link with tag="a": a link has its own semantics, and tag needs a tag that can take the semantics of a button. Style the <a> element directly when a link must look like a button.
| Prop | Type | Default |
|---|---|---|
disabled | bool | false |
focusableWhenDisabled | bool | false |
tag | string | 'button' |
children | ?Html | null |
...rest | mixed | — |
The source of the examples: examples/ui/Button.phem.