@use "@wordpress/base-styles/mixins";
@use "@wordpress/base-styles/variables" as *;

/**
 * For easier testing of potential regressions, you can use a Button variant matrix
 * available in a special Storybook instance by running `npm run storybook:e2e:dev`.
 *
 * @see https://github.com/WordPress/gutenberg/blob/trunk/test/storybook-playwright/README.md
 */

.components-button {
	// Share tone-dependent colors across variants. Fixed roles use WPDS tokens directly.
	--_wp-components-button-foreground: var(--wpds-color-foreground-interactive-brand);
	--_wp-components-button-foreground-active: var(--wpds-color-foreground-interactive-brand-active);
	--_wp-components-button-foreground-strong: var(--wpds-color-foreground-interactive-brand-strong);
	--_wp-components-button-foreground-strong-active: var(--wpds-color-foreground-interactive-brand-strong-active);
	--_wp-components-button-foreground-strong-disabled: var(--wpds-color-foreground-interactive-brand-strong-disabled);
	--_wp-components-button-background-strong: var(--wpds-color-background-interactive-brand-strong);
	--_wp-components-button-background-strong-active: var(--wpds-color-background-interactive-brand-strong-active);
	--_wp-components-button-background-strong-disabled: var(--wpds-color-background-interactive-brand-strong-disabled);
	--_wp-components-button-background-weak: var(--wpds-color-background-interactive-neutral-weak);
	--_wp-components-button-background-weak-active: var(--wpds-color-background-interactive-neutral-weak-active);
	--_wp-components-button-background-on-active: var(--_wp-components-button-background-weak-active);
	--_wp-components-button-stroke: var(--wpds-color-stroke-interactive-brand);
	--_wp-components-button-stroke-active: var(--wpds-color-stroke-interactive-brand-active);
	--_wp-components-button-foreground-busy: var(--wpds-color-foreground-interactive-neutral);

	// Defend against a conflicting focus ring from WordPress common.css on `a:focus`.
	&:focus:is(a) {
		box-shadow: none;
	}

	&:focus {
		outline: none;
	}

	&:focus:not(:active) {
		@include mixins.outset-ring__focus();
	}

	display: inline-flex;
	text-decoration: none;
	font-family: inherit;
	font-size: $default-font-size;
	font-weight: var(--wpds-typography-font-weight-emphasis);
	margin: 0;
	cursor: var(--wpds-cursor-control);
	appearance: none;
	background: none;
	height: $button-size;
	align-items: center;
	box-sizing: border-box;
	padding: 4px 12px; // Allows 32px min-height.
	border-radius: $radius-small;
	color: var(--wpds-color-foreground-interactive-neutral);
	// Every variant reserves a 1px border (transparent by default) so the box
	// size stays identical whether or not a variant paints a visible border.
	border: $border-width solid transparent;

	&.is-next-40px-default-size {
		height: $button-size-next-default-40px;
	}

	&[aria-expanded="true"],
	&:hover:not(:disabled, [aria-disabled="true"], [role^="menuitem"], [role="option"]) {
		color: var(--_wp-components-button-foreground);
	}

	/**
	 * Primary button style.
	 */

	&.is-primary {
		white-space: nowrap;
		background: var(--_wp-components-button-background-strong);
		color: var(--_wp-components-button-foreground-strong);
		text-decoration: none;
		text-shadow: none;

		&:hover:not(:disabled, [aria-disabled="true"], .is-pressed) {
			background-color: var(--_wp-components-button-background-strong-active);
			color: var(--_wp-components-button-foreground-strong-active);
		}

		&:active:not(:disabled, [aria-disabled="true"], .is-pressed) {
			background-color: var(--_wp-components-button-background-strong-active);
			color: var(--_wp-components-button-foreground-strong-active);
		}

		&:disabled,
		&[aria-disabled="true"] {
			color: var(--_wp-components-button-foreground-strong-disabled);
			background: var(--_wp-components-button-background-strong-disabled);
		}

		&.is-busy,
		&.is-busy:disabled,
		&.is-busy[aria-disabled="true"] {
			color: var(--_wp-components-button-foreground-strong);
			background-size: 100px 100%;
			/* stylelint-disable -- Disable reason: This function call looks nicer when each argument is on its own line. */
			background-image: linear-gradient(
				-45deg,
				var(--_wp-components-button-background-strong) 33%,
				var(--_wp-components-button-background-strong-active) 33%,
				var(--_wp-components-button-background-strong-active) 70%,
				var(--_wp-components-button-background-strong) 70%
			);
			/* stylelint-enable */
		}
	}

	/**
	 * Secondary and tertiary buttons.
	 */

	&.is-secondary,
	&.is-tertiary {
		--_wp-components-button-background-weak: var(--wpds-color-background-interactive-brand-weak);
		--_wp-components-button-background-weak-active: var(--wpds-color-background-interactive-brand-weak-active);
		--_wp-components-button-foreground-on-active: var(--_wp-components-button-foreground-active);
		--_wp-components-button-foreground-busy: var(--_wp-components-button-foreground-active);

		&:disabled,
		&[aria-disabled="true"] {
			color: var(--wpds-color-foreground-interactive-brand-disabled);
			background: transparent;
			transform: none;
		}
	}

	/**
	 * Secondary button style.
	 */

	&.is-secondary {
		@media not ( prefers-reduced-motion ) {
			transition: border-color 0.1s linear;
		}

		border-color: var(--_wp-components-button-stroke);
		white-space: nowrap;
		color: var(--_wp-components-button-foreground);
		background: var(--_wp-components-button-background-weak);

		&:active:not(:disabled) {
			// Hide the border while pressed, keeping its width to avoid a size shift.
			border-color: transparent;
		}

		&:hover:not(:disabled, [aria-disabled="true"], .is-pressed) {
			border-color: var(--_wp-components-button-stroke-active);
			color: var(--_wp-components-button-foreground-active);
			background-color: var(--_wp-components-button-background-on-active);
		}

		&:focus:not(:active) {
			border-color: var(--_wp-components-button-stroke);
		}

		&:disabled:not(:focus),
		&[aria-disabled="true"]:not(:focus) {
			border-color: var(--wpds-color-stroke-interactive-neutral-disabled);
		}
	}

	/**
	 * Tertiary buttons.
	 */

	&.is-tertiary {
		white-space: nowrap;
		color: var(--_wp-components-button-foreground);
		background: var(--_wp-components-button-background-weak);

		&:hover:not(:disabled, [aria-disabled="true"], .is-pressed) {
			background-color: var(--_wp-components-button-background-on-active);
			color: var(--_wp-components-button-foreground-active);
		}

		&:active:not(:disabled, [aria-disabled="true"], .is-pressed) {
			background-color: var(--_wp-components-button-background-on-active);
		}

		// Pull left if the tertiary button stands alone after a description, so as to vertically align with items above.
		p + & {
			margin-left: -($grid-unit-15 * 0.5);
		}
	}

	/**
	 * Destructive buttons.
	 */
	&.is-destructive {
		--_wp-components-button-foreground: var(--wpds-color-foreground-interactive-error);
		--_wp-components-button-foreground-active: var(--wpds-color-foreground-interactive-error-active);
		--_wp-components-button-foreground-strong: var(--wpds-color-foreground-interactive-error-strong);
		--_wp-components-button-foreground-strong-active: var(--wpds-color-foreground-interactive-error-strong-active);
		--_wp-components-button-foreground-strong-disabled: var(--wpds-color-foreground-interactive-error-strong-disabled);
		--_wp-components-button-background-strong: var(--wpds-color-background-interactive-error-strong);
		--_wp-components-button-background-strong-active: var(--wpds-color-background-interactive-error-strong-active);
		--_wp-components-button-background-strong-disabled: var(--wpds-color-background-interactive-error-strong-disabled);
		--_wp-components-button-background-weak: var(--wpds-color-background-interactive-error-weak);
		--_wp-components-button-background-weak-active: var(--wpds-color-background-interactive-error-weak-active);
		--_wp-components-button-stroke: var(--wpds-color-stroke-interactive-error);
		--_wp-components-button-stroke-active: var(--wpds-color-stroke-interactive-error-active);
		--_wp-components-button-foreground-on-active: var(--_wp-components-button-foreground-active);
		--_wp-components-button-foreground-busy: var(--_wp-components-button-foreground-active);

		// The shared pressed rule owns its foreground and background.
		// Only the default variant is styled differently from the non-destructive counterparts.
		&:not(.is-primary):not(.is-secondary):not(.is-tertiary):not(.is-link):not(.is-pressed) {
			color: var(--_wp-components-button-foreground);

			&:hover:not(:disabled, [aria-disabled="true"]) {
				color: var(--_wp-components-button-foreground-active);
			}

			&:active:not(:disabled, [aria-disabled="true"]) {
				background-color: var(--wpds-color-background-interactive-error-active);
				color: var(--_wp-components-button-foreground-active);
			}

			&:disabled,
			&[aria-disabled="true"] {
				color: var(--wpds-color-foreground-interactive-neutral-disabled);
			}
		}

		&.is-secondary {
			&:active:not(:disabled, [aria-disabled="true"], .is-pressed) {
				background-color: var(--_wp-components-button-background-on-active);
			}
		}
	}

	/**
	 * Link buttons.
	 */

	&.is-link {
		--_wp-components-button-foreground-busy: var(--_wp-components-button-foreground-active);

		margin: 0;
		padding: 0;
		border: 0;
		border-radius: 0;
		background: none;
		outline: none;
		text-align: left;
		font-weight: var(--wpds-typography-font-weight-default);
		color: var(--_wp-components-button-foreground);
		text-decoration: underline;
		text-underline-offset: 0.2em;
		text-decoration-thickness: from-font;

		@media not (prefers-reduced-motion) {
			transition-property: border, background, color;
			transition-duration: 0.05s;
			transition-timing-function: ease-in-out;
		}

		height: auto;

		&:focus:not(:active) {
			border-radius: $radius-small;
			text-decoration: none;
		}

		&:disabled,
		&[aria-disabled="true"] {
			color: var(--wpds-color-foreground-interactive-brand-disabled);
		}
	}

	&:not(:disabled, [aria-disabled="true"], .is-pressed):active {
		color: var(--_wp-components-button-foreground-on-active, var(--wpds-color-foreground-interactive-neutral-active));
	}

	&:disabled,
	&[aria-disabled="true"] {
		cursor: default;
		color: var(--wpds-color-foreground-interactive-neutral-disabled);
	}

	&.is-busy,
	&.is-secondary.is-busy,
	&.is-secondary.is-busy:disabled,
	&.is-secondary.is-busy[aria-disabled="true"] {
		--_wp-components-button-background-on-active: var(--_wp-components-button-background-weak);

		&:not(.is-primary, .is-pressed) {
			color: var(--_wp-components-button-foreground-busy);
		}

		@media not (prefers-reduced-motion) {
			animation: components-button__busy-animation 2500ms infinite linear;
		}
		background-size: 100px 100%;
		/* stylelint-disable -- Disable reason: This function call looks nicer when each argument is on its own line. */
		background-image: linear-gradient(
			-45deg,
			var(--_wp-components-button-background-weak) 33%,
			var(--_wp-components-button-background-weak-active) 33%,
			var(--_wp-components-button-background-weak-active) 70%,
			var(--_wp-components-button-background-weak) 70%
		);
		/* stylelint-enable */
	}

	&.is-compact {
		height: $button-size-compact;

		&.has-icon:not(.has-text) {
			padding: 0;
			min-width: $button-size-compact;
		}
	}

	&.is-small {
		height: var(--wpds-dimension-size-sm);
		line-height: 22px;
		padding: 0 8px;
		font-size: 11px;

		&.has-icon:not(.has-text) {
			padding: 0;
			min-width: var(--wpds-dimension-size-sm);
		}
	}

	&.has-icon {
		padding: 6px; // Works for 24px icons. Smaller icons are vertically centered by flex alignments.

		// Icon buttons are square.
		min-width: $button-size;
		justify-content: center;

		&.is-next-40px-default-size {
			min-width: $button-size-next-default-40px;
		}

		.dashicon {
			display: inline-flex;
			justify-content: center;
			align-items: center;
			padding: 2px;
			box-sizing: content-box;
		}

		&.has-text {
			justify-content: start;
			padding-right: $grid-unit-15;
			padding-left: $grid-unit-10;
			gap: $grid-unit-05;
			&.has-icon-right {
				padding-right: $grid-unit-10;
				padding-left: $grid-unit-15;
			}
		}

		// Offset the reserved 1px border so it doesn't widen the square
		// icon button.
		&:not(.has-text) {
			svg,
			.dashicon {
				margin-inline: -$border-width;
			}
		}
	}

	// Keep the pressed foreground and fill together in every interaction state.
	&.is-pressed {
		// The Toolbar paints this same background on its inset pseudo-element.
		--_wp-components-button-pressed-background: var(--wpds-color-background-interactive-neutral-strong-active);

		&,
		&:hover,
		&.is-busy {
			color: var(--wpds-color-foreground-interactive-neutral-strong-active);
			background: var(--_wp-components-button-pressed-background);
		}

		&.is-secondary:not(:disabled, [aria-disabled="true"]) {
			border-color: var(--wpds-color-stroke-interactive-neutral-active);
		}

		&:is(:disabled, [aria-disabled="true"]) {
			--_wp-components-button-pressed-background: var(--wpds-color-background-interactive-neutral-strong-disabled);

			&,
			&.is-busy {
				color: var(--wpds-color-foreground-interactive-neutral-strong-disabled);
				background: var(--_wp-components-button-pressed-background);
			}

			&.is-busy {
				--_wp-components-button-pressed-background:
					linear-gradient(-45deg, transparent 33%, var(--wpds-color-background-interactive-neutral-disabled) 33% 70%, transparent 70%)
					0 0 / 100px 100% var(--wpds-color-background-interactive-neutral-strong-disabled);
			}
		}

		// Forced colors discard the pressed colors, so fill the button with the
		// system selection colors instead. Opting out of color adjustment removes
		// the text backplate, which would otherwise cover the fill, but it also
		// lets every other color rule that matches the button take effect,
		// including those from consumers, so these rules need `!important`.
		@media ( forced-colors: active ) {
			forced-color-adjust: none;
			outline-color: CanvasText !important;
			border-color: ButtonBorder !important;
			background: SelectedItem !important;
			color: SelectedItemText !important;

			&:disabled,
			&[aria-disabled="true"] {
				border-color: GrayText !important;
				background: GrayText !important;
				color: Canvas !important;
			}
		}
	}

	svg {
		fill: currentColor;
		outline: none;
		flex-shrink: 0;
	}
}

@keyframes components-button__busy-animation {
	0% {
		background-position: 200px 0;
	}
}
