All files / components/toggle-button ToggleButton.vue

100% Statements 33/33
93.33% Branches 14/15
100% Functions 9/9
100% Lines 32/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235    2x 2x       2x     2x 2x       2x                                                                       101x 101x 101x 101x         101x 101x 115x                     101x 8x   101x 4x   101x             2x 2x 2x 2x 2x   133x             8x 8x 8x   8x   2x 2x           2x 2x                                                                                                                                                                                                                                                    
<template>
	<button
		class="cdx-toggle-button"
		:class="rootClasses"
		:aria-pressed="modelValue"
		:disabled="disabled"
		@click="onClick"
		@keydown.space.enter="setActive( true )"
		@keyup.space.enter="setActive( false )"
	>
		<!-- @slot Button content -->
		<slot />
	</button>
</template>
 
<script lang="ts">
import { ref, computed, defineComponent } from 'vue';
import useIconOnlyButton from '../../composables/useIconOnlyButton';
 
/**
 * A button that can be toggled on and off.
 */
export default defineComponent( {
	name: 'CdxToggleButton',
	props: {
		/**
		 * Whether the button should be set to "on" (true) or "off" (false).
		 *
		 * Provided by `v-model` binding in the parent component.
		 */
		modelValue: {
			type: Boolean,
			default: false
		},
		/**
		 * Whether the disabled attribute should be added to the button, which prevents
		 * it from being clicked.
		 */
		disabled: {
			type: Boolean,
			default: false
		},
		/**
		 * Whether the toggle button should be "quiet", which renders more minimally.
		 */
		quiet: {
			type: Boolean,
			default: false
		}
	},
	emits: [
		/**
		 * Emitted when modelValue changes (i.e. when the state is toggled)
		 *
		 * @property {boolean} modelValue The new model value
		 */
		'update:modelValue'
	],
	setup( props, { emit, slots, attrs } ) {
		const isIconOnly = useIconOnlyButton( slots.default, attrs, 'CdxButton' );
 
		// Support: Firefox (space), all (enter)
		// Whether the button is being pressed via the space or enter key. Needed to apply
		// consistent active styles across browsers. For mousedown/mouseup, the browser's native
		// :active state will suffice.
		const isActive = ref( false );
 
		const rootClasses = computed( () => ( {
			// Quiet means frameless among other things
			'cdx-toggle-button--quiet': props.quiet,
			'cdx-toggle-button--framed': !props.quiet,
			// Provide --toggled-off too so that we can simplify selectors
			'cdx-toggle-button--toggled-on': props.modelValue,
			'cdx-toggle-button--toggled-off': !props.modelValue,
			'cdx-toggle-button--icon-only': isIconOnly.value,
			'cdx-toggle-button--is-active': isActive.value
		} ) );
 
		const onClick = (): void => {
			emit( 'update:modelValue', !props.modelValue );
		};
 
		const setActive = ( active: boolean ) => {
			isActive.value = active;
		};
 
		return {
			rootClasses,
			onClick,
			setActive
		};
	}
} );
</script>
 
<style lang="less">
@import ( reference ) '@wikimedia/codex-design-tokens/theme-wikimedia-ui.less';
@import ( reference ) '../../themes/mixins/button.less';
 
// Common styles for framed and quiet versions
.cdx-toggle-button {
	// mixin for common base styles for buttons
	.cdx-mixin-button();
 
	&:enabled {
		color: @color-base;
 
		&:hover {
			// Use hand cursor. This is nonstandard for a button but allows for a visible
			// interactivity distinction from the disabled state.
			cursor: @cursor-base--hover;
		}
 
		&:focus {
			border-color: @border-color-progressive--focus;
			box-shadow: @box-shadow-inset-small @box-shadow-color-progressive--focus;
			// In Windows high contrast mode the outline becomes visible.
			outline: @outline-base--focus;
		}
 
		&:active,
		&.cdx-toggle-button--is-active {
			color: @color-emphasized;
			border-color: @border-color-interactive;
			box-shadow: none;
		}
	}
 
	.cdx-icon {
		// Any icons used in the toggle button content should have the same color as the
		// rest of the content. This overrides the color rule in Icon.vue.
		color: inherit;
		// TODO: The vertical alignment in the current DOM structure is not ideal. The icon should
		// be aligned with the text baseline, but this doesn't work well in our multi font-size
		// skin setup in MediaWiki. This is a temporary compromise to be resolved with either
		// skin specific CSS or a DOM structure change. See T326900.
		vertical-align: middle;
	}
}
 
// Framed version
.cdx-toggle-button--framed {
	&:enabled {
		background-color: @background-color-interactive-subtle;
		border-color: @border-color-base;
 
		&:hover {
			background-color: @background-color-base;
			color: @color-base--hover;
		}
 
		// Enabled + active state has some shared styles for quiet and framed, see above.
		&:active,
		&.cdx-toggle-button--is-active {
			background-color: @background-color-interactive;
		}
 
		// &:enabled:focus has some shared styles for quiet and framed, see above.
	}
 
	/* stylelint-disable-next-line no-descending-specificity */
	&:disabled {
		background-color: @background-color-disabled;
		color: @color-inverted;
		border-color: @border-color-disabled;
	}
 
	&.cdx-toggle-button--toggled-on:enabled {
		background-color: @background-color-progressive--active;
		color: @color-inverted-fixed;
		border-color: @border-color-progressive--active;
 
		&:hover {
			background-color: @background-color-progressive--hover;
			// Same color as not hovering, needed to override the other :hover rule above
			color: @color-inverted-fixed;
			border-color: @border-color-progressive--hover;
		}
 
		&:focus {
			border-color: @border-color-progressive--focus;
			// Make `box-shadow` feature a `1px` White inset outline with a value combination.
			/* stylelint-disable-next-line stylistic/declaration-colon-newline-after,
				stylistic/value-list-comma-newline-after */
			box-shadow: @box-shadow-inset-small @box-shadow-color-progressive--focus,
				@box-shadow-inset-medium @box-shadow-color-inverted;
		}
 
		&:active,
		&.cdx-toggle-button--is-active {
			background-color: @background-color-interactive;
			// Repeat general enabled + active styles here, to override more specific
			// color/border-color/box-shadow rules
			color: @color-emphasized;
			border-color: @border-color-interactive;
			box-shadow: none;
		}
	}
}
 
// Quiet toggle button
.cdx-toggle-button--quiet {
	// Override `<button>` default background color.
	background-color: @background-color-transparent;
	border-color: @border-color-transparent;
 
	&:enabled {
		&.cdx-toggle-button--toggled-on {
			background-color: @background-color-interactive;
		}
 
		&:hover {
			background-color: @background-color-interactive-subtle;
		}
 
		// `&:enabled:focus` has some shared styles for quiet and framed, see above.
		&:focus {
			background-color: @background-color-interactive-subtle;
		}
 
		// Enabled + active state has some shared styles for quiet and framed, see above
		&:active,
		&.cdx-toggle-button--is-active {
			background-color: @background-color-interactive;
		}
	}
 
	// No difference between disabled toggled-on and toggled-off.
	/* stylelint-disable-next-line no-descending-specificity */
	&:disabled {
		color: @color-disabled;
	}
}
</style>