# ConfirmOrCancelButtons

Pair of buttons that closes a form or a dialog: a text "Cancel" button and a filled "Save" button, aligned to the end, with optional content (such as "Unsaved changes") at the start. When the space they sit in is narrower than 480px (a phone, a narrow drawer) they stack at full width, with the confirm button on top; `direction="row"` or `direction="column"` fixes the layout. In the actions of a `PageHeader` they stay in a row unless `direction="column"`. `loading` shows a spinner on the confirm button and disables it; `confirmType="submit"` makes it submit the surrounding form. Both buttons can be replaced with snippets that receive the handlers. It has no space above it, like every component (the layout decides, for example with `gap`); `--confirm-or-cancel-buttons-margin-top` adds it, and setting `--confirm-or-cancel-buttons-default-margin-top` in the app CSS gives it to every instance, as v4 did with 20px. In the actions of a `Dialog` or a `Drawer`, which have their own padding, it is always 0. Its state is exposed as `data-loading` and `data-direction` for app CSS.

```ts
import { ConfirmOrCancelButtons } from '@likable-hair/svelte';
```

Category: composed. Source: `src/lib/components/composed/forms/ConfirmOrCancelButtons.svelte`.

## Examples

### Basic

`onconfirmClick` and `oncancelClick` receive the native click event. While `loading` the confirm button shows a spinner and is disabled; `confirmDisable` and `cancelDisable` disable one button.

```svelte
<script lang="ts">
	import { ConfirmOrCancelButtons } from '@likable-hair/svelte';

	let saving = $state(false);
	let message = $state('');

	function save() {
		saving = true;
		message = '';
		setTimeout(() => {
			saving = false;
			message = 'Saved';
		}, 1200);
	}
</script>

<div class="demo">
	<ConfirmOrCancelButtons
		loading={saving}
		onconfirmClick={save}
		oncancelClick={() => (message = 'Cancelled')}
	/>
	<ConfirmOrCancelButtons confirmText="Publish" cancelText="Back" confirmDisable />
	<p aria-live="polite">{message}</p>
</div>

<style>
	.demo {
		display: grid;
		gap: 16px;
		width: 100%;
	}

	p {
		min-height: 1lh;
		margin: 0;
		font-size: 13px;
	}
</style>
```

### Content and icons

`children` sits at the start of the row, for example a note about unsaved changes. `confirmIcon`, `cancelIcon` and `confirmVariant` change the buttons; `danger` suits a delete.

```svelte
<script lang="ts">
	import { mdiCheck, mdiDeleteOutline, mdiInformationOutline } from '@mdi/js';
	import { ConfirmOrCancelButtons, Icon } from '@likable-hair/svelte';
</script>

<div class="demo">
	<div class="bar">
		<ConfirmOrCancelButtons confirmText="Confirm" confirmIcon={mdiCheck}>
			<Icon path={mdiInformationOutline} />
			Unsaved changes
		</ConfirmOrCancelButtons>
	</div>
	<div class="bar">
		<ConfirmOrCancelButtons
			confirmText="Delete"
			confirmVariant="danger"
			confirmIcon={mdiDeleteOutline}
		/>
	</div>
</div>

<style>
	.demo {
		display: grid;
		gap: 16px;
		width: 100%;
		max-width: 520px;
	}

	.bar {
		padding: 14px 16px;
		border: 1px solid var(--global-color-border);
		border-radius: var(--global-radius-lg);
		background: var(--global-color-surface-solid);
	}
</style>
```

### In a narrow form

In a space narrower than 480px the buttons stack at full width, confirm on top, whatever the size of the screen. `confirmType="submit"` submits the form, so its validation runs first.

```svelte
<script lang="ts">
	import { ConfirmOrCancelButtons, SimpleTextField } from '@likable-hair/svelte';

	let sent = $state('');

	function submit(event: SubmitEvent & { currentTarget: HTMLFormElement }) {
		event.preventDefault();
		sent = new URLSearchParams(new FormData(event.currentTarget) as never).toString();
	}
</script>

<form class="panel" onsubmit={submit}>
	<SimpleTextField label="Customer name" name="name" required />
	<ConfirmOrCancelButtons confirmType="submit" oncancelClick={() => (sent = '')} />
	{#if sent}<code>{sent}</code>{/if}
</form>

<style>
	.panel {
		display: grid;
		gap: 20px;
		width: 100%;
		max-width: 320px;
		padding: 16px;
		border: 1px solid var(--global-color-border);
		border-radius: var(--global-radius-lg);
	}
</style>
```

### Direction

By default the buttons stack only when their space is narrower than 480px. `direction="column"` always stacks them, confirm on top at full width, for example under a login form or in a profile card; `direction="row"` keeps them in a row even in a narrow space.

```svelte
<script lang="ts">
	import { ConfirmOrCancelButtons } from '@likable-hair/svelte';
</script>

<div class="demo">
	<div class="card">
		<ConfirmOrCancelButtons direction="column" confirmText="Log out" cancelText="Reload profile" />
	</div>
	<div class="narrow">
		<ConfirmOrCancelButtons direction="row" confirmText="Apply" cancelText="Reset" />
	</div>
</div>

<style>
	.demo {
		display: flex;
		flex-wrap: wrap;
		align-items: flex-start;
		gap: 16px;
	}

	.card {
		width: 320px;
		padding: 16px;
		border: 1px solid var(--global-color-border);
		border-radius: var(--global-radius-lg);
		background: var(--global-color-surface-solid);
	}

	.narrow {
		width: 240px;
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `loading` | `boolean` | `false` | Shows a spinner on the confirm button and disables it. |
| `confirmText` | `string` |  | Text of the confirm button. Defaults to "Save", in the language of `locale`. |
| `cancelText` | `string` |  | Text of the cancel button. Defaults to "Cancel", in the language of `locale`. |
| `locale` | `string` |  | BCP 47 locale of the texts: `it` and `en` are built in, other languages fall back to English. Defaults to the one set with `setDefaultLocale`, else `en`. |
| `confirmDisable` | `boolean` | `false` | Disables the confirm button. |
| `cancelDisable` | `boolean` | `false` | Disables the cancel button. |
| `confirmIcon` | `string` |  | SVG path of an icon in the confirm button, for example from `@mdi/js`. |
| `cancelIcon` | `string` |  | SVG path of an icon in the cancel button. |
| `confirmVariant` | `ComponentProps<typeof Button>['variant']` | `'primary'` | Color of the confirm button, for example `danger` for a delete. |
| `confirmType` | `'button' \| 'submit'` | `'button'` | Native `type` of the confirm button. `submit` submits the surrounding form, with its validation. |
| `direction` | `'auto' \| 'row' \| 'column'` | `'auto'` | How the buttons are laid out. `auto` keeps them in a row and stacks them at full width, confirm on top, when the space is narrower than 480px; `row` never stacks them; `column` always stacks them. |
| `class` | `{ container?: string; content?: string; actions?: string; cancel?: string; confirm?: string; }` | `{}` | Extra classes for each part. |

It also accepts the native attributes and events of `<div>` (for example `id`, `title`, `aria-label`, `onkeydown`), which are forwarded to the element.

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `children` |  | Content at the start of the row, before the buttons (for example "Unsaved changes"). |
| `cancelButtonSnippet` | `{ loading: boolean; handleCancel: (event: MouseEvent \| KeyboardEvent) => void; cancelText: string; cancelDisable: boolean; }` | Replaces the cancel button. Call `handleCancel` from your button. |
| `confirmButtonSnippet` | `{ loading: boolean; handleConfirm: (event: MouseEvent) => void; confirmText: string; confirmDisable: boolean; }` | Replaces the confirm button. Call `handleConfirm` from your button. |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onconfirmClick` | `(event: MouseEvent) => void` | Called when the confirm button is clicked. |
| `oncancelClick` | `(event: MouseEvent \| KeyboardEvent) => void` | Called when the cancel button is clicked. |

## CSS variables

For a single instance, pass them as style props: `<ConfirmOrCancelButtons --confirm-or-cancel-buttons-content-color="tomato">`. The `--confirm-or-cancel-buttons-default-*` defaults apply to the whole app: override them in `:root` or in a theme.

| Per-instance variable | Default |
| --- | --- |
| `--confirm-or-cancel-buttons-margin-top` | `--confirm-or-cancel-buttons-default-margin-top`: `0px` |
| `--confirm-or-cancel-buttons-gap` | `--confirm-or-cancel-buttons-default-gap`: `12px` |
| `--confirm-or-cancel-buttons-content-gap` | `--confirm-or-cancel-buttons-default-content-gap`: `6px` |
| `--confirm-or-cancel-buttons-content-color` | `--confirm-or-cancel-buttons-default-content-color`: `var(--global-color-text-3)` |
| `--confirm-or-cancel-buttons-content-font-size` | `--confirm-or-cancel-buttons-default-content-font-size`: `13px` |
| `--confirm-or-cancel-buttons-actions-gap` | `--confirm-or-cancel-buttons-default-actions-gap`: `8px` |
