UNPKG

10.1 kBSCSSView Raw
1// Foundation for Sites by ZURB
2// foundation.zurb.com
3// Licensed under MIT Open Source
4
5////
6/// @group button
7////
8
9/// Font family for button elements.
10/// @type Font
11$button-font-family: inherit !default;
12
13/// Padding inside buttons.
14/// @type List
15$button-padding: 0.85em 1em !default;
16
17/// Margin around buttons.
18/// @type List
19$button-margin: 0 0 $global-margin 0 !default;
20
21/// Default fill for buttons. Can either be `solid` or `hollow`.
22/// @type Keyword
23$button-fill: solid !default;
24
25/// Default background color for buttons.
26/// @type Color
27$button-background: $primary-color !default;
28
29/// Background color on hover for buttons.
30/// @type Color
31$button-background-hover: scale-color($button-background, $lightness: -15%) !default;
32
33/// Font color for buttons.
34/// @type List
35$button-color: $white !default;
36
37/// Alternative font color for buttons.
38/// @type List
39$button-color-alt: $black !default;
40
41/// Border radius for buttons, defaulted to global-radius.
42/// @type Number
43$button-radius: $global-radius !default;
44
45/// Border width for hollow outline buttons
46/// @type Number
47$button-hollow-border-width: 1px !default;
48
49/// Sizes for buttons.
50/// @type Map
51$button-sizes: (
52 tiny: 0.6rem,
53 small: 0.75rem,
54 default: 0.9rem,
55 large: 1.25rem,
56) !default;
57
58/// Coloring classes. A map of classes to output in your CSS, like `.secondary`, `.success`, and so on.
59/// @type Map
60$button-palette: $foundation-palette !default;
61
62/// opacity for a disabled button.
63/// @type List
64$button-opacity-disabled: 0.25 !default;
65
66/// Background color lightness on hover for buttons.
67/// @type Number
68$button-background-hover-lightness: -20% !default;
69
70/// Color lightness on hover for hollow buttons.
71/// @type Number
72$button-hollow-hover-lightness: -50% !default;
73
74// Internal: flip from margin-right to margin-left for defaults
75@if $global-text-direction == 'rtl' {
76 $button-margin: 0 0 $global-margin $global-margin !default;
77}
78
79/// transitions for buttons.
80/// @type List
81$button-transition: background-color 0.25s ease-out, color 0.25s ease-out !default;
82
83// TODO: Document button-base() mixin
84@mixin button-base {
85 @include disable-mouse-outline;
86 display: inline-block;
87 vertical-align: middle;
88 margin: $button-margin;
89 font-family: $button-font-family;
90
91 @if (type-of($button-padding) == 'map') {
92 @each $size, $padding in $button-padding {
93 @include breakpoint($size) {
94 padding: $padding;
95 }
96 }
97 }
98 @else {
99 padding: $button-padding;
100 }
101
102 -webkit-appearance: none;
103 border: 1px solid transparent;
104 border-radius: $button-radius;
105 transition: $button-transition;
106
107 font-size: map-get($button-sizes, default);
108 line-height: 1;
109 text-align: center;
110 cursor: pointer;
111}
112
113/// Expands a button to make it full-width.
114/// @param {Boolean} $expand [true] - Set to `true` to enable the expand behavior. Set to `false` to reverse this behavior.
115@mixin button-expand($expand: true) {
116 @if $expand {
117 display: block;
118 width: 100%;
119 margin-right: 0;
120 margin-left: 0;
121 }
122 @else {
123 display: inline-block;
124 width: auto;
125 margin: $button-margin;
126 }
127}
128
129/// Sets the visual style of a button.
130/// @param {Color} $background [$button-background] - Background color of the button.
131/// @param {Color} $background-hover [$button-background-hover] - Background color of the button on hover. Set to `auto` to have the mixin automatically generate a hover color.
132/// @param {Color} $color [$button-color] - Text color of the button. Set to `auto` to automatically generate a color based on the background color.
133@mixin button-style(
134 $background: $button-background,
135 $background-hover: $button-background-hover,
136 $color: $button-color,
137 $background-hover-lightness: $button-background-hover-lightness
138) {
139 @if $color == auto {
140 $color: color-pick-contrast($background, ($button-color, $button-color-alt));
141 }
142
143 @if $background-hover == auto {
144 $background-hover: scale-color($background, $lightness: $background-hover-lightness);
145 }
146
147 background-color: $background;
148 color: $color;
149
150 &:hover, &:focus {
151 background-color: $background-hover;
152 color: $color;
153 }
154}
155
156/// Removes background fill on hover and focus for hollow buttons.
157@mixin button-hollow {
158 &,
159 &:hover, &:focus {
160 background-color: transparent;
161 }
162
163 &.disabled,
164 &[disabled] {
165 &,
166 &:hover, &:focus {
167 background-color: transparent;
168 }
169 }
170}
171
172@mixin button-hollow-style(
173 $color: $button-background,
174 $hover-lightness: $button-hollow-hover-lightness,
175 $border-width: $button-hollow-border-width
176) {
177 $color-hover: scale-color($color, $lightness: $hover-lightness);
178
179 border: $border-width solid $color;
180 color: $color;
181
182 &:hover, &:focus {
183 border-color: $color-hover;
184 color: $color-hover;
185 &.disabled,
186 &[disabled] {
187 border: $border-width solid $color;
188 color: $color;
189 }
190 }
191}
192
193/// Adds disabled styles to a button by fading the element, reseting the cursor, and disabling pointer events.
194/// @param [Color] $background [$button-background] - Background color of the disabled button.
195/// @param [Color] $color [$button-color] - Text color of the disabled button. Set to `auto` to have the mixin automatically generate a color based on the background color.
196@mixin button-disabled(
197 $background: $button-background,
198 $color: $button-color
199) {
200 @if $color == auto {
201 $color: color-pick-contrast($background, ($button-color, $button-color-alt));
202 }
203
204 opacity: $button-opacity-disabled;
205 cursor: not-allowed;
206
207 &, &:hover, &:focus {
208 background-color: $background;
209 color: $color;
210 }
211}
212
213/// Adds a dropdown arrow to a button.
214/// @param {Number} $size [0.4em] - Size of the arrow. We recommend using an `em` value so the triangle scales when used inside different sizes of buttons.
215/// @param {Color} $color [white] - Color of the arrow.
216/// @param {Number} $offset [$button-padding] - Distance between the arrow and the text of the button. Defaults to whatever the right padding of a button is.
217@mixin button-dropdown(
218 $size: 0.4em,
219 $color: $white,
220 $offset: get-side($button-padding, right)
221) {
222 &::after {
223 @include css-triangle($size, $color, down);
224 position: relative;
225 top: 0.4em; // Aligns the arrow with the text of the button
226
227 display: inline-block;
228 float: #{$global-right};
229 margin-#{$global-left}: $offset;
230 }
231}
232
233/// Adds all styles for a button. For more granular control over styles, use the individual button mixins.
234/// @param {Boolean} $expand [false] - Set to `true` to make the button full-width.
235/// @param {Color} $background [$button-background] - Background color of the button.
236/// @param {Color} $background-hover [$button-background-hover] - Background color of the button on hover. Set to `auto` to have the mixin automatically generate a hover color.
237/// @param {Color} $color [$button-color] - Text color of the button. Set to `auto` to automatically generate a color based on the background color.
238/// @param {Keyword} $style [solid] - Set to `hollow` to create a hollow button. The color defined in `$background` will be used as the primary color of the button.
239@mixin button(
240 $expand: false,
241 $background: $button-background,
242 $background-hover: $button-background-hover,
243 $color: $button-color,
244 $style: $button-fill
245) {
246 @include button-base;
247
248 @if $style == solid {
249 @include button-style($background, $background-hover, $color);
250 }
251 @else if $style == hollow {
252 @include button-hollow;
253 @include button-hollow-style($background);
254 }
255
256 @if $expand {
257 @include button-expand;
258 }
259}
260
261@mixin foundation-button {
262 .button {
263 @include button;
264
265 // Sizes
266 @each $size, $value in map-remove($button-sizes, default) {
267 &.#{$size} {
268 font-size: $value;
269 }
270 }
271
272 &.expanded { @include button-expand; }
273
274 // Colors
275 @each $name, $color in $button-palette {
276 @if $button-fill != hollow {
277 &.#{$name} {
278 @include button-style($color, auto, auto);
279 }
280 }
281 @else {
282 &.#{$name} {
283 @include button-hollow-style($color);
284 }
285
286 &.#{$name}.dropdown::after {
287 border-top-color: $color;
288 }
289 }
290 }
291
292 // Disabled style
293 &.disabled,
294 &[disabled] {
295 @include button-disabled;
296
297 @each $name, $color in $button-palette {
298 &.#{$name} {
299 @include button-disabled($color, auto);
300 }
301 }
302 }
303
304 // Hollow style
305 @if $button-fill != hollow {
306 &.hollow {
307 @include button-hollow;
308 @include button-hollow-style;
309
310 @each $name, $color in $button-palette {
311 &.#{$name} {
312 @include button-hollow-style($color);
313 }
314 }
315 }
316 }
317
318 // Clear style
319 @if $button-fill != clear {
320 &.clear {
321 @include button-hollow;
322 @include button-hollow-style;
323
324 &, &:hover, &:focus {
325 &, &.disabled, &[disabled] {
326 border-color: transparent;
327 }
328 }
329
330 @each $name, $color in $button-palette {
331 &.#{$name} {
332 @include button-hollow-style($color);
333
334 &, &:hover,
335 &:focus {
336 &,
337 &.disabled,
338 &[disabled] {
339 border-color: transparent;
340 }
341 }
342 }
343 }
344 }
345 }
346
347 // Dropdown arrow
348 &.dropdown {
349 @include button-dropdown;
350
351 @if $button-fill == hollow {
352 &::after {
353 border-top-color: $button-background;
354 }
355 }
356
357 &.hollow {
358 &::after {
359 border-top-color: $button-background;
360 }
361
362 @each $name, $color in $button-palette {
363 &.#{$name} {
364 &::after {
365 border-top-color: $color;
366 }
367 }
368 }
369 }
370 }
371
372 // Button with dropdown arrow only
373 &.arrow-only::after {
374 top: -0.1em;
375 float: none;
376 margin-#{$global-left}: 0;
377 }
378 }
379 a.button {
380 &:hover,
381 &:focus {
382 text-decoration: none;
383 }
384 }
385}