Skip to main content
Version: v10.0.0-beta.1

ontario-button

Use buttons to help the user carry out an important action such as starting a transaction or agreeing to a purchase.

Usage guidance​

Please refer to the Ontario Design System for current documentation guidance.

Disabled state​

This component intentionally does not provide a disabled prop.

Disabling action controls can create accessibility and usability barriers, and often does not explain what the user needs to fix.

Instead:

  • keep actions available
  • use validation and error messaging to clearly identify missing or invalid input

When used in forms, pair button behavior with component-level error handling guidance in related form fields.

Source: https://designsystem.ontario.ca/components/detail/buttons.html#disabled-buttons

Configuration​

Once the component package has been installed (see Ontario Design System Component Library for installation instructions), the button component can be added directly into the project's code, and can be customized by updating the properties outlined here. Please see the examples below for how to configure the component.

Examples​

Example of a bare-bones button component where the label for the button is provided. The default button type will be secondary, and the native HTML button type will be button.

<ontario-button>Element Content</ontario-button>
Element Content

Example of a button component, which includes the label, elementId, htmlType, type and ariaLabelText properties.

<ontario-button aria-label-text="Example aria label text" element-id="button-id" html-type="button" type="tertiary">
Element Content
</ontario-button>

Element Content

Example of a button component rendering as a native link when href is provided. Use this pattern for navigation, and keep htmlType for action and form-submit use cases.

<ontario-button href="/services" target="_blank" rel="noreferrer" type="primary">Browse services</ontario-button>
Browse services

This is another example of a button component, where the user is passing in the label through the label property - overriding the 'Element Content' value. The resulting label text will display as Example.

<ontario-button label="Example" html-type="submit" type="primary">Element Content</ontario-button>
Element Content

An example of how to pass in an onclick function to the button component.

<ontario-button onclick="exampleFunction()">Element Content</ontario-button>

An example of a button with slotted SVG content. When no label is provided, the default slot is used to display the content. Because there is no visible text, pair it with ariaLabelText so the button has an accessible name.

<ontario-button aria-label-text="Open map">
<svg width="24" height="24" viewBox="0 0 24 24" aria-hidden="true" focusable="false">
<path d="M12 2a7 7 0 0 0-7 7c0 5 7 13 7 13s7-8 7-13a7 7 0 0 0-7-7Z" fill="currentColor" />
</svg>
</ontario-button>

Icons​

Icons can be added alongside a label (or text content) using the leading-icon and trailing-icon named slots. Either slot can be used on its own, or both together; whichever side isn't used simply collapses its spacing automatically.

Prefer an ontario-icon-* component (see ontario-icon) with colour="inherit", so the icon automatically tracks the button's current text colour — including hover/focus/active state changes — instead of needing a colour set per button type. Always mark icons as decorative with isDecorative/is-decorative="true" so they aren't announced twice by assistive technology (the button's accessible name already comes from label/text content or ariaLabelText).

<ontario-button label="Add item" type="primary">
<ontario-icon-add slot="leading-icon" colour="inherit" is-decorative="true"></ontario-icon-add>
</ontario-button>

The same pattern applies to trailing-icon, or both slots together for an icon on each side of the label:

<ontario-button label="Download report" type="primary">
<ontario-icon-download slot="leading-icon" colour="inherit" is-decorative="true"></ontario-icon-download>
<ontario-icon-chevron-right slot="trailing-icon" colour="inherit" is-decorative="true"></ontario-icon-chevron-right>
</ontario-button>

Slotted icons must be direct children of the button, because the browser only assigns direct children to a slot. When wrapping an icon (as in the React example), put the slot attribute on the wrapper.

In React, wrap each icon in a <span slot="..."> (a plain element that carries the slot attribute) rather than passing slot directly to the icon component. The browser only slots direct children of <ontario-button>, and a Stencil component rendered through the React wrapper isn't a direct child that carries slot.

A raw <svg>/<img> can also be slotted in directly (e.g. for a custom icon not covered by ontario-icon-*); it is sized to 24x24px automatically:

<ontario-button label="Add item" type="primary">
<svg slot="leading-icon" aria-hidden="true" focusable="false" viewBox="0 0 24 24">
<path fill="currentColor" d="M11 5h2v6h6v2h-6v6h-2v-6H5v-2h6z" />
</svg>
</ontario-button>

Icon-only buttons​

An icon-only button (no visible label) is detected automatically when a default-slotted element (e.g. an ontario-icon-* component or an SVG) is provided without label/text content. Icon-only buttons render as a compact square, rather than the full-width rectangle used for labelled buttons. Always provide ariaLabelText in this mode, since there's no visible text to derive an accessible name from.

<ontario-button aria-label-text="Open map" type="primary">
<ontario-icon-map colour="inherit" is-decorative="true"></ontario-icon-map>
</ontario-button>

Icon-only buttons scale their icon up to 32px on mobile (and to 28px for the tertiary type on desktop) to keep a consistent tap-target size. A raw slotted <svg>/<img> is resized automatically by the button's CSS to match; an ontario-icon-* component sizes itself internally via its own iconWidth/icon-width prop, so set that explicitly per breakpoint/type if you need an exact match (CSS can't reach inside its shadow DOM to resize it).

Sizing​

Buttons with icons are sized as follows:

  • 24px of padding between the button edge and the label or icon. For a secondary button the 2px border counts towards this, so the 24px is measured to its outer edge.
  • 8px between an icon and the label.
  • A minimum width of 160px, with no maximum width on desktop, so a button grows with its content.

On mobile, every button is at most 328px wide, and the label wraps within a maximum width so the icons keep their spacing:

ConfigurationMaximum label width
No icon280px
One icon (leading or trailing)248px
Two icons (leading and trailing)216px

A secondary button with no icon keeps its existing padding, with the border added on top, so its label is 4px narrower than the table (276px) on mobile.

Forms​

The ontario-button supports integration with native HTML <form> elements. This element integrates with the underlying browser form API and should work the same as adding an <input type="submit">.

To use the ontario-button as a submit button set the htmlType to "submit". This will wire the ontario-button up to the <form> it is in and allow it to perform the submit. Internally, the ontario-button will fire a submit event on its parent <form> and trigger the native behaviour of the browser.

When href is provided, the component renders as a native link instead. In that mode, htmlType is ignored and should not be used for form submission.

<form>
<!-- Add other form elements, either native or design system elements -->
<ontario-input
id="input-1"
caption="What is your name?"
required
name="input-1"
hint-text="Enter your full name"
></ontario-input>

<!-- Submit the form with a submit button -->
<ontario-button type="primary" html-type="submit">Submit</ontario-button>
</form>

Alternatively, adding a click listener with a custom handler will allow for any action to be performed with the ontario-button. To trigger the submit the <form> element can programmatically have the submit event dispatched to it.

<form id="form-1">
<!-- Add other form elements, either native or design system elements -->
<ontario-input
id="input-1"
caption="What is your name?"
required
name="input-1"
hint-text="Enter your full name"
></ontario-input>

<!-- Submit the form with a submit button -->
<ontario-button type="primary">Submit</ontario-button>
</form>

<script>
window.addEventListener('load', () => {
// Look up the elements within the document
const form = document.getElementById('form-1');
const button = document.getElementById('input-1');

// Submit the form with an event, if `submit()` is used it can't be
// prevented using `preventDefault()`
button.addEventListener('click', () => {
form?.dispatchEvent(new Event('submit', { cancelable: true }));
});

// Perform what ever actions are needed when submitting the form,
// for example, validation
form.addEventListener('submit', (event) => {
// Perform necessary logic, eg. validation

// Sample: emit form data to the console
const formData = new FormData(event.target);
console.log('Form data:', formData);

// Avoid an actual submission if submission isn't desired (optional)
event.preventDefault();
});
});
</script>

Accessibility​

It is important to know when the proper use-case is for a button. Often, the roles of links and buttons can get confused with one another.

  • Links will redirect you to a new page or another section/component on a page.
  • Buttons will trigger an action, such as opening or closing a modal, or submitting a form.

It can be confusing and frustrating for users to expect a button to trigger an action, and then they are redirected to a new page.

Technical Note: SSR (Server-Side Rendering) Considerations​

The Ontario Button component supports server-side rendering, with a few considerations:

  • Preferred content source: Pass button text through the label prop.
  • Slotted content caveat: Slotted children rely on fallback host.textContent, which is not reliably available during SSR.
  • Framework guidance: For deterministic SSR output, prefer label over slotted children.
  • Slotted icons in Next.js: wrap each icon in a <span slot="..."> rather than passing slot to the icon component. See Icons above.

SSR-safe example:​

<ontario-button label="Click me"></ontario-button>

Overview​

Ontario Button triggers actions and supports button or link behavior.

This component intentionally does not expose a disabled prop.

To support accessible and understandable form completion:

  • keep actions available
  • use validation and error messaging to guide corrections instead of disabling

For component guidance, see:

Disabled/read-only policy source:

Icons​

Icons can be added alongside a label/text content using the leading-icon and trailing-icon named slots. Either slot (or both) can be used; only one of the two slotted sides needs an icon for the other to still collapse its spacing automatically.

Prefer an ontario-icon-* component with colour="inherit", so the icon automatically tracks the button's current text colour (including hover/focus/active state changes) instead of needing a colour set per button type. A raw <svg>/<img> can also be slotted in directly (e.g. for a custom icon); it is sized to 24x24px automatically.

Icon-only buttons (no visible label) are detected automatically when a default-slotted element (e.g. an ontario-icon-* component or an SVG) is provided without a label/text content. In this mode, always provide ariaLabelText so the button remains accessible.

Properties​

PropertyAttributeDescriptionTypeDefault
ariaLabelTextaria-label-textProvides more context as to what the button interaction is doing. This should only be used for accessibility purposes, if the button interaction requires more description than what the text provides. This is optional.string | undefinedundefined
elementIdelement-idThe unique identifier of the button. This is optional - if no ID is passed, one will be generated.string | undefinedundefined
hrefhrefWhen provided, the component renders as a native anchor for navigation use cases. This takes precedence over htmlType, so form-submission behaviour is disabled in link mode.string | undefinedundefined
htmlTypehtml-typeThe native HTML button type the button should use. If no htmlType is passed, it will default to 'button'. This prop only affects the component when it renders as a native <button>. If href is provided, the component renders as a native <a> and htmlType is ignored."button" | "reset" | "submit"'button'
labellabelText to be displayed within the button. This will override the text provided through the host element textContent.string | undefinedundefined
relrelSpecifies the relationship of the linked document to the current document when href is provided. This prop has no effect unless the component is in link mode.string | undefinedundefined
targettargetSpecifies where to open the linked document when href is provided. This prop has no effect unless the component is in link mode.string | undefinedundefined
typetypeThe type of button to render. If no type is passed, it will default to 'secondary'."internalThemeDark" | "primary" | "secondary" | "tertiary"'secondary'

Slots​

SlotDescription
Content to display inside the button when no label is provided (for example, an inline <svg>). Pair it with ariaLabelText when there is no visible text.
"leading-icon"An icon displayed before the label.
"trailing-icon"An icon displayed after the label.

Built with StencilJS