2018-10-23 03:26:07 +02:00
|
|
|
---
|
2019-09-03 01:32:14 +02:00
|
|
|
title: sass:list
|
2018-10-23 03:26:07 +02:00
|
|
|
---
|
|
|
|
|
2019-09-03 01:32:14 +02:00
|
|
|
<%= partial '../snippets/built-in-module-status' %>
|
|
|
|
|
2018-10-23 03:26:07 +02:00
|
|
|
<% fun_fact do %>
|
2018-10-23 22:42:40 +02:00
|
|
|
In Sass, every [map][] counts as a list that contains a two-element list for
|
|
|
|
each key/value pair. For example, `(1: 2, 3: 4)` counts as `(1 2, 3 4)`. So
|
|
|
|
all these functions work for maps as well!
|
2018-10-23 03:26:07 +02:00
|
|
|
|
2018-10-23 22:42:40 +02:00
|
|
|
[map]: ../values/maps
|
2018-10-23 03:26:07 +02:00
|
|
|
|
2018-10-23 22:42:40 +02:00
|
|
|
Individual values also count as lists. All these functions treat `1px` as a
|
|
|
|
list that contains the value `1px`.
|
2018-10-23 03:26:07 +02:00
|
|
|
<% end %>
|
|
|
|
|
|
|
|
|
2019-09-03 01:32:14 +02:00
|
|
|
<% function 'list.append($list, $val, $separator: auto)',
|
|
|
|
'append($list, $val, $separator: auto)',
|
|
|
|
returns: 'list' do %>
|
2018-10-23 22:42:40 +02:00
|
|
|
Returns a copy of `$list` with `$val` added to the end.
|
|
|
|
|
2021-05-21 03:09:28 +02:00
|
|
|
If `$separator` is `comma`, `space`, or `slash`, the returned list is
|
|
|
|
comma-separated, space-separated, or slash-separated, respectively. If it's
|
|
|
|
`auto` (the default), the returned list will use the same separator as `$list`
|
|
|
|
(or `space` if `$list` doesn't have a separator). Other values aren't allowed.
|
2018-10-23 22:42:40 +02:00
|
|
|
|
|
|
|
[separator]: ../values/lists
|
|
|
|
|
2019-09-03 01:32:14 +02:00
|
|
|
Note that unlike [`list.join()`](#join), if `$val` is a list it's nested
|
|
|
|
within the returned list rather than having all its elements added to the
|
|
|
|
returned list.
|
2018-10-23 22:42:40 +02:00
|
|
|
|
|
|
|
<% example(autogen_css: false) do %>
|
2019-09-03 01:32:14 +02:00
|
|
|
@debug list.append(10px 20px, 30px); // 10px 20px 30px
|
|
|
|
@debug list.append((blue, red), green); // blue, red, green
|
|
|
|
@debug list.append(10px 20px, 30px 40px); // 10px 20px (30px 40px)
|
|
|
|
@debug list.append(10px, 20px, $separator: comma); // 10px, 20px
|
|
|
|
@debug list.append((blue, red), green, $separator: space); // blue red green
|
2018-10-23 22:42:40 +02:00
|
|
|
===
|
2019-09-03 01:32:14 +02:00
|
|
|
@debug list.append(10px 20px, 30px) // 10px 20px 30px
|
|
|
|
@debug list.append((blue, red), green) // blue, red, green
|
|
|
|
@debug list.append(10px 20px, 30px 40px) // 10px 20px (30px 40px)
|
|
|
|
@debug list.append(10px, 20px, $separator: comma) // 10px, 20px
|
|
|
|
@debug list.append((blue, red), green, $separator: space) // blue red green
|
2018-10-23 22:42:40 +02:00
|
|
|
<% end %>
|
2018-10-23 03:26:07 +02:00
|
|
|
<% end %>
|
|
|
|
|
|
|
|
|
2019-09-03 01:32:14 +02:00
|
|
|
<% function 'list.index($list, $value)',
|
|
|
|
'index($list, $value)',
|
|
|
|
returns: 'number | null' do %>
|
2018-10-23 22:42:40 +02:00
|
|
|
Returns the [index][] of `$value` in `$list`.
|
2018-10-23 03:26:07 +02:00
|
|
|
|
2018-10-23 22:42:40 +02:00
|
|
|
[index]: ../values/lists#indexes
|
2018-10-23 03:26:07 +02:00
|
|
|
|
2018-10-23 22:42:40 +02:00
|
|
|
If `$value` doesn't appear in `$list`, this returns [`null`][]. If `$value`
|
|
|
|
appears multiple times in `$list`, this returns the index of its first
|
|
|
|
appearance.
|
2018-10-23 03:26:07 +02:00
|
|
|
|
2018-10-23 22:42:40 +02:00
|
|
|
[`null`]: ../values/null
|
2018-10-23 03:26:07 +02:00
|
|
|
|
2018-10-23 22:42:40 +02:00
|
|
|
<%= partial 'code-snippets/example-list-index' %>
|
2018-10-23 03:26:07 +02:00
|
|
|
<% end %>
|
|
|
|
|
|
|
|
|
2019-09-03 01:32:14 +02:00
|
|
|
<% function 'list.is-bracketed($list)',
|
|
|
|
'is-bracketed($list)',
|
|
|
|
returns: 'boolean' do %>
|
2018-10-23 22:42:40 +02:00
|
|
|
Returns whether `$list` has square brackets.
|
|
|
|
|
|
|
|
<% example(autogen_css: false) do %>
|
2019-09-03 01:32:14 +02:00
|
|
|
@debug list.is-bracketed(1px 2px 3px); // false
|
|
|
|
@debug list.is-bracketed([1px, 2px, 3px]); // true
|
2018-10-23 22:42:40 +02:00
|
|
|
===
|
2019-09-03 01:32:14 +02:00
|
|
|
@debug list.is-bracketed(1px 2px 3px) // false
|
|
|
|
@debug list.is-bracketed([1px, 2px, 3px]) // true
|
2018-10-23 22:42:40 +02:00
|
|
|
<% end %>
|
2018-10-23 03:26:07 +02:00
|
|
|
<% end %>
|
|
|
|
|
|
|
|
|
2019-09-03 01:32:14 +02:00
|
|
|
<% function 'list.join($list1, $list2, $separator: auto, $bracketed: auto)',
|
|
|
|
'join($list1, $list2, $separator: auto, $bracketed: auto)',
|
|
|
|
returns: 'list' do %>
|
2018-10-23 22:42:40 +02:00
|
|
|
Returns a new list containing the elements of `$list1` followed by the elements
|
|
|
|
of `$list2`.
|
|
|
|
|
|
|
|
<% heads_up do %>
|
|
|
|
Because individual values count as single-element lists, it's possible to
|
2019-09-03 01:32:14 +02:00
|
|
|
use `list.join()` to add a value to the end of a list. However, *this is not
|
2018-10-23 22:42:40 +02:00
|
|
|
recommended*, since if that value is a list it will be concatenated, which
|
|
|
|
is probably not what you're expecting.
|
|
|
|
|
2019-09-03 01:32:14 +02:00
|
|
|
Use [`list.append()`](#append) instead to add a single value to a list. Only
|
|
|
|
use `list.join()` to combine two lists together into one.
|
2018-10-23 22:42:40 +02:00
|
|
|
<% end %>
|
|
|
|
|
2021-05-21 03:09:28 +02:00
|
|
|
If `$separator` is `comma`, `space`, or `slash`, the returned list is
|
|
|
|
comma-separated, space-separated, or slash-separated, respectively. If it's
|
|
|
|
`auto` (the default), the returned list will use the same separator as
|
|
|
|
`$list1` if it has a separator, or else `$list2` if it has a separator, or
|
|
|
|
else `space`. Other values aren't allowed.
|
2018-10-23 22:42:40 +02:00
|
|
|
|
|
|
|
If `$bracketed` is `true`, the returned list has square brackets. If it's
|
|
|
|
`false`, the returned list has no brackets. If it's `auto` (the default), the
|
|
|
|
returned list will be bracketed if `$list1` is. Other values aren't allowed.
|
|
|
|
|
|
|
|
<% example(autogen_css: false) do %>
|
2019-09-03 01:32:14 +02:00
|
|
|
@debug list.join(10px 20px, 30px 40px); // 10px 20px 30px 40px
|
|
|
|
@debug list.join((blue, red), (#abc, #def)); // blue, red, #abc, #def
|
|
|
|
@debug list.join(10px, 20px); // 10px 20px
|
|
|
|
@debug list.join(10px, 20px, $separator: comma); // 10px, 20px
|
|
|
|
@debug list.join((blue, red), (#abc, #def), $separator: space); // blue red #abc #def
|
|
|
|
@debug list.join([10px], 20px); // [10px 20px]
|
|
|
|
@debug list.join(10px, 20px, $bracketed: true); // [10px 20px]
|
2018-10-23 22:42:40 +02:00
|
|
|
===
|
2019-09-03 01:32:14 +02:00
|
|
|
@debug list.join(10px 20px, 30px 40px) // 10px 20px 30px 40px
|
|
|
|
@debug list.join((blue, red), (#abc, #def)) // blue, red, #abc, #def
|
|
|
|
@debug list.join(10px, 20px) // 10px 20px
|
|
|
|
@debug list.join(10px, 20px, comma) // 10px, 20px
|
|
|
|
@debug list.join((blue, red), (#abc, #def), space) // blue red #abc #def
|
|
|
|
@debug list.join([10px], 20px) // [10px 20px]
|
|
|
|
@debug list.join(10px, 20px, $bracketed: true) // [10px 20px]
|
2018-10-23 22:42:40 +02:00
|
|
|
<% end %>
|
2018-10-23 03:26:07 +02:00
|
|
|
<% end %>
|
|
|
|
|
|
|
|
|
2019-09-03 01:32:14 +02:00
|
|
|
<% function 'list.length($list)', 'length($list)', returns: 'number' do %>
|
2018-10-23 22:42:40 +02:00
|
|
|
Returns the length of `$list`.
|
|
|
|
|
|
|
|
This can also return the number of pairs in a map.
|
|
|
|
|
|
|
|
<% example(autogen_css: false) do %>
|
2019-09-03 01:32:14 +02:00
|
|
|
@debug list.length(10px); // 1
|
|
|
|
@debug list.length(10px 20px 30px); // 3
|
|
|
|
@debug list.length((width: 10px, height: 20px)); // 2
|
2018-10-23 22:42:40 +02:00
|
|
|
===
|
2019-09-03 01:32:14 +02:00
|
|
|
@debug list.length(10px) // 1
|
|
|
|
@debug list.length(10px 20px 30px) // 3
|
|
|
|
@debug list.length((width: 10px, height: 20px)) // 2
|
2018-10-23 22:42:40 +02:00
|
|
|
<% end %>
|
2018-10-23 03:26:07 +02:00
|
|
|
<% end %>
|
|
|
|
|
|
|
|
|
2019-09-03 01:32:14 +02:00
|
|
|
<% function 'list.separator($list)',
|
|
|
|
'list-separator($list)',
|
|
|
|
returns: 'unquoted string' do %>
|
2021-05-21 03:09:28 +02:00
|
|
|
Returns the name of the separator used by `$list`, either `space`, `comma`, or
|
|
|
|
`slash`.
|
2018-10-23 22:42:40 +02:00
|
|
|
|
|
|
|
If `$list` doesn't have a separator, returns `space`.
|
|
|
|
|
|
|
|
<% example(autogen_css: false) do %>
|
2019-09-03 01:32:14 +02:00
|
|
|
@debug list.separator(1px 2px 3px); // space
|
|
|
|
@debug list.separator(1px, 2px, 3px); // comma
|
|
|
|
@debug list.separator('Helvetica'); // space
|
|
|
|
@debug list.separator(()); // space
|
2018-10-23 22:42:40 +02:00
|
|
|
===
|
2019-09-03 01:32:14 +02:00
|
|
|
@debug list.separator(1px 2px 3px) // space
|
|
|
|
@debug list.separator(1px, 2px, 3px) // comma
|
|
|
|
@debug list.separator('Helvetica') // space
|
|
|
|
@debug list.separator(()) // space
|
2018-10-23 22:42:40 +02:00
|
|
|
<% end %>
|
2018-10-23 03:26:07 +02:00
|
|
|
<% end %>
|
|
|
|
|
|
|
|
|
2019-09-03 01:32:14 +02:00
|
|
|
<% function 'list.nth($list, $n)', 'nth($list, $n)' do %>
|
2018-10-23 22:42:40 +02:00
|
|
|
Returns the element of `$list` at [index][] `$n`.
|
2018-10-23 03:26:07 +02:00
|
|
|
|
2018-10-23 22:42:40 +02:00
|
|
|
[index]: ../values/lists#indexes
|
2018-10-23 03:26:07 +02:00
|
|
|
|
2018-10-23 22:42:40 +02:00
|
|
|
If `$n` is negative, it counts from the end of `$list`. Throws an error if
|
|
|
|
there is no element at index `$n`.
|
2018-10-23 03:26:07 +02:00
|
|
|
|
2018-10-23 22:42:40 +02:00
|
|
|
<%= partial 'code-snippets/example-list-nth' %>
|
2018-10-23 03:26:07 +02:00
|
|
|
<% end %>
|
|
|
|
|
|
|
|
|
2019-09-03 01:32:14 +02:00
|
|
|
<% function 'list.set-nth($list, $n, $value)',
|
|
|
|
'set-nth($list, $n, $value)',
|
|
|
|
returns: 'list' do %>
|
2018-10-23 22:42:40 +02:00
|
|
|
Returns a copy of `$list` with the element at [index][] `$n` replaced with
|
|
|
|
`$value`.
|
2018-10-23 03:26:07 +02:00
|
|
|
|
2018-10-23 22:42:40 +02:00
|
|
|
[index]: ../values/lists#indexes
|
2018-10-23 03:26:07 +02:00
|
|
|
|
2018-10-23 22:42:40 +02:00
|
|
|
If `$n` is negative, it counts from the end of `$list`. Throws an error if
|
|
|
|
there is no existing element at index `$n`.
|
2018-10-23 03:26:07 +02:00
|
|
|
|
2018-10-23 22:42:40 +02:00
|
|
|
<% example(autogen_css: false) do %>
|
2019-09-03 01:32:14 +02:00
|
|
|
@debug list.set-nth(10px 20px 30px, 1, 2em); // 2em 20px 30px
|
|
|
|
@debug list.set-nth(10px 20px 30px, -1, 8em); // 10px, 20px, 8em
|
|
|
|
@debug list.set-nth((Helvetica, Arial, sans-serif), 3, Roboto); // Helvetica, Arial, Roboto
|
2018-10-23 22:42:40 +02:00
|
|
|
===
|
2019-09-03 01:32:14 +02:00
|
|
|
@debug list.set-nth(10px 20px 30px, 1, 2em); // 2em 20px 30px
|
|
|
|
@debug list.set-nth(10px 20px 30px, -1, 8em); // 10px, 20px, 8em
|
|
|
|
@debug list.set-nth((Helvetica, Arial, sans-serif), 3, Roboto); // Helvetica, Arial, Roboto
|
2018-10-23 22:42:40 +02:00
|
|
|
<% end %>
|
2018-10-23 03:26:07 +02:00
|
|
|
<% end %>
|
|
|
|
|
|
|
|
|
2021-05-21 03:09:28 +02:00
|
|
|
<% function 'list.slash($elements...)', returns: 'list' do %>
|
|
|
|
Returns a slash-separated list that contains `$elements`.
|
|
|
|
|
|
|
|
<% heads_up do %>
|
|
|
|
This function is a temporary solution for creating slash-separated lists.
|
|
|
|
Eventually, they'll be written literally with slashes, as in
|
|
|
|
`1px / 2px / solid`, but for the time being [slashes are used for division]
|
|
|
|
so Sass can't use them for new syntax until the old syntax is removed.
|
|
|
|
|
|
|
|
[slashes are used for division]: ../breaking-changes/slash-div
|
|
|
|
<% end %>
|
|
|
|
|
|
|
|
<% example(autogen_css: false) do %>
|
|
|
|
@debug list.slash(1px, 50px, 100px); // 10px / 50px / 100px
|
|
|
|
===
|
|
|
|
@debug list.slash(1px, 50px, 100px) // 10px / 50px / 100px
|
|
|
|
<% end %>
|
|
|
|
<% end %>
|
|
|
|
|
|
|
|
|
2019-09-03 01:32:14 +02:00
|
|
|
<% function 'list.zip($lists...)', 'zip($lists...)', returns: 'list' do %>
|
2018-10-23 22:42:40 +02:00
|
|
|
Combines every list in `$lists` into a single list of sub-lists.
|
2018-10-23 03:26:07 +02:00
|
|
|
|
2018-10-23 22:42:40 +02:00
|
|
|
Each element in the returned list contains all the elements at that position
|
|
|
|
in `$lists`. The returned list is as long as the shortest list in `$lists`.
|
2018-10-23 03:26:07 +02:00
|
|
|
|
2018-10-23 22:42:40 +02:00
|
|
|
The returned list is always comma-separated and the sub-lists are always
|
|
|
|
space-separated.
|
2018-10-23 03:26:07 +02:00
|
|
|
|
2018-10-23 22:42:40 +02:00
|
|
|
<% example(autogen_css: false) do %>
|
2019-09-03 01:32:14 +02:00
|
|
|
@debug list.zip(10px 50px 100px, short mid long); // 10px short, 50px mid, 100px long
|
|
|
|
@debug list.zip(10px 50px 100px, short mid); // 10px short, 50px mid
|
2018-10-23 22:42:40 +02:00
|
|
|
===
|
2019-09-03 01:32:14 +02:00
|
|
|
@debug list.zip(10px 50px 100px, short mid long) // 10px short, 50px mid, 100px long
|
|
|
|
@debug list.zip(10px 50px 100px, short mid) // 10px short, 50px mid
|
2018-10-23 22:42:40 +02:00
|
|
|
<% end %>
|
2018-10-23 03:26:07 +02:00
|
|
|
<% end %>
|