_shape.scss 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. //
  2. // Copyright 2018 Google Inc.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. @use 'sass:list';
  23. @use 'sass:map';
  24. @use 'sass:math';
  25. @use 'sass:meta';
  26. @use '@material/feature-targeting/feature-targeting';
  27. @use '@material/rtl/rtl';
  28. @use '@material/theme/custom-properties';
  29. @use '@material/theme/css';
  30. @use '@material/theme/keys';
  31. @use '@material/theme/theme';
  32. // Shape categories
  33. $small-component-radius: 4px !default;
  34. $medium-component-radius: 4px !default;
  35. $large-component-radius: 0 !default;
  36. @include keys.set-values(
  37. (
  38. small: $small-component-radius,
  39. medium: $medium-component-radius,
  40. large: $large-component-radius,
  41. ),
  42. $options: (custom-property-prefix: shape)
  43. );
  44. // @deprecated use shape.resolve-radius() to retrieve a value or
  45. // shape.get-shape-keys() and shape.is-shape-key() to manipulate keys.
  46. $category-keywords: (
  47. small: keys.create-custom-property(small),
  48. medium: keys.create-custom-property(medium),
  49. large: keys.create-custom-property(large),
  50. ) !default;
  51. @function is-shape-key($radius) {
  52. @return map.has-key($category-keywords, $radius);
  53. }
  54. @function get-shape-keys() {
  55. @return map.keys($category-keywords);
  56. }
  57. //
  58. // Flips the radius values based on RTL context.
  59. //
  60. // Examples:
  61. //
  62. // 1. mdc-shape-flip-radius((0, 4px, 4px, 0)) => 4px 0 0 4px
  63. // 2. mdc-shape-flip-radius((0, 8px)) => 8px 0
  64. //
  65. @function flip-radius($radius) {
  66. @if meta.type-of($radius) == 'list' {
  67. @if list.length($radius) > 4 {
  68. @error "Invalid radius: '#{$radius}' is more than 4 values";
  69. }
  70. }
  71. @if list.length($radius) == 4 {
  72. @return list.nth($radius, 2) list.nth($radius, 1) list.nth($radius, 4)
  73. list.nth($radius, 3);
  74. } @else if list.length($radius) == 3 {
  75. @return list.nth($radius, 2) list.nth($radius, 1) list.nth($radius, 2)
  76. list.nth($radius, 3);
  77. } @else if list.length($radius) == 2 {
  78. @return list.nth($radius, 2) list.nth($radius, 1);
  79. } @else {
  80. @return $radius;
  81. }
  82. }
  83. /// Returns the resolved radius value of a shape category - `large`, `medium`,
  84. /// or `small`. If $radius is not a category, this function returns the value
  85. /// itself if valid. Valid values are numbers or percentages.
  86. ///
  87. /// If a percentage is provided, $component-height should be specified if the
  88. /// width of the component does not match the height.
  89. ///
  90. /// $radius may be a single value or a list of 1 to 4 values.
  91. ///
  92. /// @example - scss
  93. /// resolve-radius(small); // (varname: --mdc-shape-small, fallback: 4px)
  94. /// resolve-radius((varname: --custom-shape, fallback: small));
  95. /// // (
  96. /// // varname: --custom-shape,
  97. /// // fallback: (varname: --mdc-shape-small, fallback: 4px)
  98. /// // )
  99. /// resolve-radius(8px); // 8px
  100. /// resolve-radius(50%, $component-height: 36px); // 16px
  101. ///
  102. /// $shape: (
  103. /// family: 'rounded',
  104. /// radius: (
  105. /// 8px,
  106. /// 8px,
  107. /// 8px,
  108. /// 8px,
  109. /// ),
  110. /// );
  111. /// resolve-radius($shape); // 8px
  112. ///
  113. /// @param {String | Number | Map | List} $radius - the radius shape category or
  114. /// radius value to resolve. May be a number, custom property Map, shape
  115. /// Map, or a List of those values.
  116. /// @return {Number | Map | List} the resolved radius value. May be a number,
  117. /// a custom property Map, or a List if $radius was a List.
  118. @function resolve-radius($radius, $component-height: null) {
  119. @if $radius == null {
  120. @return null;
  121. }
  122. @if meta.type-of($radius) == 'list' {
  123. // $radius is a List
  124. @if list.length($radius) > 4 or list.length($radius) < 1 {
  125. @error "mdc-shape: Invalid radius: #{$radius}. Radius must be between 1 and 4 values.";
  126. }
  127. $radii: ();
  128. @each $corner in $radius {
  129. $radii: list.append(
  130. $radii,
  131. resolve-radius($corner, $component-height: $component-height)
  132. );
  133. }
  134. @return $radii;
  135. }
  136. @if is-shape-key($radius) {
  137. // $radius is a shape key
  138. @return resolve-radius(
  139. keys.create-custom-property($radius),
  140. $component-height: $component-height
  141. );
  142. } @else if custom-properties.is-custom-prop($radius) {
  143. // $radius is a custom property Map
  144. $fallback: resolve-radius(
  145. custom-properties.get-fallback($radius, $shallow: true),
  146. $component-height: $component-height
  147. );
  148. @return custom-properties.set-fallback($radius, $fallback, $shallow: true);
  149. } @else if meta.type-of($radius) == 'map' {
  150. // $radius is a shape Map
  151. $family: map.get($radius, family);
  152. @if custom-properties.is-custom-prop($family) {
  153. // Shape Map may have been passed through keys.create-custom-properties()
  154. $family: custom-properties.get-fallback($family);
  155. }
  156. @if $family != 'rounded' {
  157. @error 'mdc-shape: Invalid shape family: "#{$family}". Only "rounded" is supported.';
  158. }
  159. @return resolve-radius(
  160. map.get($radius, radius),
  161. $component-height: $component-height
  162. );
  163. } @else {
  164. // $radius is a value
  165. @if meta.type-of($radius) != 'number' {
  166. @error "mdc-shape: Invalid radius: #{$radius}. Must be a number.";
  167. }
  168. $radius-unit: math.unit($radius);
  169. $component-height-type: meta.type-of($component-height);
  170. @if $radius-unit == '%' and $component-height-type == 'number' {
  171. $radius: _resolve-radius-percentage($radius, $component-height);
  172. }
  173. @return $radius;
  174. }
  175. }
  176. //
  177. // Accepts radius number or list of 2-4 radius values and returns 4 value list with
  178. // masked corners as mentioned in `$masked-corners`
  179. //
  180. // Example:
  181. //
  182. // 1. mdc-shape-mask-radius(2px 3px, 1 1 0 0) => 2px 3px 0 0
  183. // 2. mdc-shape-mask-radius(8px, 0 0 1 1) => 0 0 8px 8px
  184. // 3. mdc-shape-mask-radius(4px 4px 4px 4px, 0 1 1 0) => 0 4px 4px 0
  185. //
  186. @function mask-radius($radius, $masked-corners) {
  187. @if meta.type-of($radius) == 'list' {
  188. @if list.length($radius) > 4 {
  189. @error "Invalid radius: '#{$radius}' is more than 4 values";
  190. }
  191. }
  192. @if list.length($masked-corners) != 4 {
  193. @error "Expected masked-corners of length 4 but got '#{list.length($masked-corners)}'.";
  194. }
  195. $radius: unpack-radius($radius);
  196. @return if(list.nth($masked-corners, 1) == 1, list.nth($radius, 1), 0)
  197. if(list.nth($masked-corners, 2) == 1, list.nth($radius, 2), 0)
  198. if(list.nth($masked-corners, 3) == 1, list.nth($radius, 3), 0)
  199. if(list.nth($masked-corners, 4) == 1, list.nth($radius, 4), 0);
  200. }
  201. /// Unpacks shorthand values for border-radius (i.e. lists of 1-3 values).
  202. /// If a list of 4 values is given, it is returned as-is.
  203. ///
  204. /// Examples:
  205. ///
  206. /// shape.unpack-radius(4px) => 4px 4px 4px 4px
  207. /// shape.unpack-radius(4px 2px) => 4px 2px 4px 2px
  208. /// shape.unpack-radius(4px 2px 2px) => 4px 2px 2px 2px
  209. /// shape.unpack-radius(4px 2px 0 2px) => 4px 2px 0 2px
  210. ///
  211. /// @param {Number | Map | List} $radius - List of 1 to 4 radius numbers.
  212. /// @return {List} a List of 4 radius numbers.
  213. @function unpack-radius($radius) {
  214. @return css.unpack-value($radius);
  215. }
  216. /// Resolve a percentage radius into a number.
  217. ///
  218. /// @param {Number} $percentage - the radius percentage.
  219. /// @param {Number} $component-height - the height of the component.
  220. /// @return {Number} the resolved radius as a number.
  221. @function _resolve-radius-percentage($percentage, $component-height) {
  222. // Converts the percentage to number without unit. Example: 50% => 50.
  223. $percentage: math.div($percentage, $percentage * 0 + 1);
  224. @return $component-height * math.div($percentage, 100);
  225. }
  226. @mixin radius(
  227. $radius,
  228. $rtl-reflexive: false,
  229. $component-height: null,
  230. $query: feature-targeting.all()
  231. ) {
  232. $feat-structure: feature-targeting.create-target($query, structure);
  233. @include feature-targeting.targets($feat-structure) {
  234. $has-multiple-corners: meta.type-of($radius) == 'list' and
  235. list.length($radius) > 1;
  236. // Even if $rtl-reflexive is true, only emit RTL styles if we can't easily tell that the given radius is symmetrical
  237. $needs-flip: $rtl-reflexive and $has-multiple-corners;
  238. $radius: resolve-radius($radius, $component-height: $component-height);
  239. @if not $has-multiple-corners {
  240. @include theme.property(border-radius, $radius);
  241. } @else {
  242. $gss: (
  243. noflip: $needs-flip,
  244. );
  245. $radii: unpack-radius($radius);
  246. @include theme.property(
  247. border-top-left-radius,
  248. list.nth($radii, 1),
  249. $gss: $gss
  250. );
  251. @include theme.property(
  252. border-top-right-radius,
  253. list.nth($radii, 2),
  254. $gss: $gss
  255. );
  256. @include theme.property(
  257. border-bottom-right-radius,
  258. list.nth($radii, 3),
  259. $gss: $gss
  260. );
  261. @include theme.property(
  262. border-bottom-left-radius,
  263. list.nth($radii, 4),
  264. $gss: $gss
  265. );
  266. }
  267. @if ($needs-flip) {
  268. @include rtl.rtl {
  269. // If it needs to be flipped, it will always have multiple corners
  270. $gss: (
  271. noflip: true,
  272. );
  273. $radii: flip-radius(unpack-radius($radius));
  274. @include theme.property(
  275. border-top-left-radius,
  276. list.nth($radii, 1),
  277. $gss: $gss
  278. );
  279. @include theme.property(
  280. border-top-right-radius,
  281. list.nth($radii, 2),
  282. $gss: $gss
  283. );
  284. @include theme.property(
  285. border-bottom-right-radius,
  286. list.nth($radii, 3),
  287. $gss: $gss
  288. );
  289. @include theme.property(
  290. border-bottom-left-radius,
  291. list.nth($radii, 4),
  292. $gss: $gss
  293. );
  294. }
  295. }
  296. }
  297. }
  298. /// Resolves one or more shape tokens and expands them into 4 separate logical
  299. /// tokens for each corner.
  300. ///
  301. /// @example - scss
  302. /// $theme: (container-shape: (4px 4px 0 0));
  303. /// $theme: resolve-theme(
  304. /// $theme,
  305. /// map.get($resolvers, shape),
  306. /// container-shape,
  307. /// );
  308. /// // (
  309. /// // container-shape-start-start: 4px,
  310. /// // container-shape-start-end: 4px,
  311. /// // container-shape-end-end: 0,
  312. /// // container-shape-end-start: 0,
  313. /// // )
  314. ///
  315. /// @param {Map} $theme - The theme to resolve tokens for.
  316. /// @param {Function} $resolver - The shape resolver to use.
  317. /// @param {String...} $shape-tokens - The shape tokens to resolve.
  318. /// @return {Map} The theme with resolved shape tokens.
  319. @function resolve-theme($theme, $resolver, $shape-tokens...) {
  320. @if $resolver == null {
  321. @return $theme;
  322. }
  323. @each $token in $shape-tokens {
  324. $shape-theme: meta.call($resolver, $shape: map.get($theme, $token));
  325. // Add resolved values, but allow $theme to override the results if needed.
  326. $theme: map.merge(
  327. (
  328. '#{$token}-start-start': map.get($shape-theme, start-start),
  329. '#{$token}-start-end': map.get($shape-theme, start-end),
  330. '#{$token}-end-end': map.get($shape-theme, end-end),
  331. '#{$token}-end-start': map.get($shape-theme, end-start),
  332. ),
  333. $theme
  334. );
  335. $theme: map.remove($theme, $token);
  336. }
  337. @return $theme;
  338. }
  339. /// Resolves a shape value by expanding it into logical values for each corner.
  340. ///
  341. /// @example - scss
  342. ///
  343. /// resolver($shape: (4px 4px 0 0)) =>
  344. /// (
  345. /// start-start: 4px,
  346. /// start-end: 4px,
  347. /// end-end: 0,
  348. /// end-start: 0,
  349. /// )
  350. ///
  351. /// resolver($shape: 4px) =>
  352. /// (
  353. /// start-start: 4px,
  354. /// start-end: 4px,
  355. /// end-end: 4px,
  356. /// end-start: 4px,
  357. /// )
  358. /// @param {Number|List} $shape - The shape token's value.
  359. /// @return {Map} A map with logical tokens for each corner's value.
  360. @function resolver($shape) {
  361. @if meta.type-of($shape) != 'list' {
  362. @return (
  363. start-start: $shape,
  364. start-end: $shape,
  365. end-end: $shape,
  366. end-start: $shape
  367. );
  368. }
  369. @if list.length($shape) == 1 {
  370. @return (
  371. start-start: list.nth($shape, 1),
  372. start-end: list.nth($shape, 1),
  373. end-end: list.nth($shape, 1),
  374. end-start: list.nth($shape, 1)
  375. );
  376. }
  377. @if list.length($shape) == 2 {
  378. @return (
  379. start-start: list.nth($shape, 1),
  380. start-end: list.nth($shape, 2),
  381. end-end: list.nth($shape, 1),
  382. end-start: list.nth($shape, 2)
  383. );
  384. }
  385. @if list.length($shape) == 3 {
  386. @return (
  387. start-start: list.nth($shape, 1),
  388. start-end: list.nth($shape, 2),
  389. end-end: list.nth($shape, 3),
  390. end-start: list.nth($shape, 2)
  391. );
  392. }
  393. @return (
  394. start-start: list.nth($shape, 1),
  395. start-end: list.nth($shape, 2),
  396. end-end: list.nth($shape, 3),
  397. end-start: list.nth($shape, 4)
  398. );
  399. }