UNPKG

8.41 kBMarkdownView Raw
1<!--docs:
2title: "Grid Lists"
3layout: detail
4section: components
5excerpt: "An RTL-aware Material Design grid list component."
6iconId: card
7path: /catalog/grid-lists/
8-->
9
10## Important - Deprecation Notice
11
12The existing `MDCGridList` component and styles will be removed in a future release. Some of its functionality
13will be available in the [MDC Image List](../mdc-image-list) package instead. Bugs and feature requests
14will no longer be accepted for the `mdc-grid-list` package. It is recommended that you migrate to the
15`mdc-image-list` package to continue to receive new features and updates.
16
17# Grid Lists
18
19MDC Grid List provides a RTL-aware Material Design Grid list component adhering to the Material Design Grid list spec.
20Grid Lists are best suited for presenting homogeneous data, typically images.
21Each item in a grid list is called a **tile**. Tiles maintain consistent width, height, and padding
22across screen sizes.
23
24## Installation
25
26```
27npm install @material/grid-list
28```
29
30
31## Usage
32
33Basic Grid list has the following structure:
34
35```html
36<div class="mdc-grid-list">
37 <ul class="mdc-grid-list__tiles">
38 <li class="mdc-grid-tile">
39 <div class="mdc-grid-tile__primary">
40 <img class="mdc-grid-tile__primary-content" src="my-image.jpg" />
41 </div>
42 <span class="mdc-grid-tile__secondary">
43 <span class="mdc-grid-tile__title">Title</span>
44 </span>
45 </li>
46 <li class="mdc-grid-tile">
47 <div class="mdc-grid-tile__primary">
48 <img class="mdc-grid-tile__primary-content" src="my-image.jpg" />
49 </div>
50 <span class="mdc-grid-tile__secondary">
51 <span class="mdc-grid-tile__title">Title</span>
52 </span>
53 </li>
54 </ul>
55</div>
56```
57
58The above markup will give you a Grid list of tiles that:
59
60- Have 4px padding in between themselves
61- Have a 1x1 aspect ratio
62- Have a one-line footer caption with no icon
63
64You just need to put the content you want to load in `src` of
65`<img class="mdc-grid-tile__primary-content" src="..."/>`. However, if your
66assets don't have the same aspect ratio you as specified in the tile, it will
67distort those assets. We provide a solution of that case in
68[Using a div in place of an img](#using-a-div-in-place-of-an-img) section.
69
70
71### Setting the tile width
72
73The tile width is set to 200px by default. There are three ways that you can
74overwrite the default value for your grid list:
75
761. Using CSS variables
77
78 ```css
79 .mdc-grid-tile {
80 --mdc-grid-list-tile-width: 300px;
81 }
82 ```
83
842. Overwriting SCSS variable
85
86 You can overwrite the scss variable by
87
88 ```scss
89 $mdc-grid-list-tile-width: 300px;
90 @import "@material/grid-list/mdc-grid-list";
91 ```
92
933. Add own style to tile
94
95 ```html
96 <style>
97 .my-grid-list .mdc-grid-tile {
98 width : 300px;
99 }
100 </style>
101 <div class="mdc-grid-list my-grid-list">
102 <ul class="mdc-grid-list__tiles">
103 <li class="mdc-grid-tile"></li>
104 ...
105 </ul>
106 </div>
107 ```
108
109### Change tile padding
110
111Grid list tiles can have 1px padding instead of 4px by adding
112`mdc-grid-list--tile-gutter-1` modifier.
113
114```html
115<div class="mdc-grid-list mdc-grid-list--tile-gutter-1">
116 <ul class="mdc-grid-list__tiles">
117 ...
118 </ul>
119</div>
120```
121
122### Image only tile
123
124Grid lists support image only tile. You can remove `mdc-grid-tile__secondary`
125and create a image only grid list.
126
127```html
128<div class="mdc-grid-list mdc-grid-list--tile-gutter-1">
129 <ul class="mdc-grid-list__tiles">
130 <li class="mdc-grid-tile">
131 <div class="mdc-grid-tile__primary">
132 <img class="mdc-grid-tile__primary-content" src="images/1-1.jpg" />
133 </div>
134 </li>
135 </ul>
136</div>
137```
138
139### Header caption
140
141Grid lists support header caption. You can change the footer caption to be a
142header caption by adding `mdc-grid-list--header-caption` modifier.
143
144```html
145<div class="mdc-grid-list mdc-grid-list--header-caption">
146 <ul class="mdc-grid-list__tiles">
147 ...
148 </ul>
149</div>
150```
151
152### Add support text to secondary content (caption)
153
154Grid lists support a one-line caption by default. You can add an additional line of support
155text if needed by adding the `mdc-grid-list--twoline-caption` modifier and additional
156markup
157
158```html
159<div class="mdc-grid-list mdc-grid-list--twoline-caption">
160 <ul class="mdc-grid-list__tiles">
161 <li class="mdc-grid-tile">
162 <div class="mdc-grid-tile__primary">
163 <img class="mdc-grid-tile__primary-content" src="my-image.jpg" />
164 </div>
165 <span class="mdc-grid-tile__secondary">
166 <span class="mdc-grid-tile__title">Title</span>
167 <span class="mdc-grid-tile__support-text">Support text</span>
168 </span>
169 </li>
170 </ul>
171</div>
172```
173
174### Add icon to secondary content (caption)
175
176You can add an icon to a caption by adding `mdc-grid-list--with-icon-align-start` or
177`mdc-grid-list--with-icon-align-end` and changing the markup.
178
179```html
180<div class="mdc-grid-list mdc-grid-list--with-icon-align-start">
181 <ul class="mdc-grid-list__tiles">
182 <li class="mdc-grid-tile">
183 <div class="mdc-grid-tile__primary">
184 <img class="mdc-grid-tile__primary-content" src="my-image.jpg" />
185 </div>
186 <span class="mdc-grid-tile__secondary">
187 <i class="mdc-grid-tile__icon material-icons">star_border</i>
188 <span class="mdc-grid-tile__title">Title</span>
189 </span>
190 </li>
191 </ul>
192</div>
193```
194
195### Change aspect ratio of tile
196
197Grid list tiles support all material guideline recommended aspect ratio:
198
199- 1x1
200- 16x9
201- 2x3
202- 3x2
203- 4x3
204- 3x4
205
206You can use the modifier class `mdc-grid-list--tile-aspect-$ASPECT_RATIO` to apply these aspect
207ratios to your grid list. Simply replace `$ASPECT_RATIO` with any of the predefined ratios.
208
209```html
210<!-- Example of 16x9 tile -->
211<div class="mdc-grid-list mdc-grid-list--tile-aspect-16x9">
212 <ul class="mdc-grid-list__tiles">
213 ...
214 </ul>
215</div>
216```
217
218As pointed out in the previous section, if your
219assets don't have the same aspect ratio you as specified in the tile, it will
220distort those assets. We provide a solution of that case in
221[Using a div in place of an img](#using-a-div-in-place-of-an-img) section.
222
223### Using a div in place of an img
224
225In case you cannot ensure all your assets will have the same aspect ratio, you
226can use `div` instead of `img` markup. It will resize the assets to cover the tile
227and crop the assets to display the center part.
228
229```html
230<style>
231 .my-tile-image {
232 background-image: url(my-image.jpg);
233 }
234</style>
235
236<div class="mdc-grid-list">
237 <ul class="mdc-grid-list__tiles">
238 <li class="mdc-grid-tile">
239 <div class="mdc-grid-tile__primary">
240 <div class="mdc-grid-tile__primary-content my-tile-image"></div>
241 </div>
242 <span class="mdc-grid-tile__secondary">
243 <span class="mdc-grid-tile__title">Title</span>
244 </span>
245 </li>
246 </ul>
247</div>
248```
249
250However, the method results in a less semantic markup, so we don't use this method by
251default.
252
253### RTL Support
254
255`mdc-grid-list` is automatically RTL-aware, and will re-position elements whenever
256it, or its ancestors, have a `dir="rtl"` attribute.
257
258### Theme
259
260`mdc-grid-list` supports theming. `mdc-grid-tile__primary` uses the theme's background
261color for its background color. `mdc-grid-tile__secondary` uses the theme's primary
262color for its background color, and the theme's `on-primary` color for its text color.
263
264### `MDCGridListFoundation`
265
266Method Signature | Description
267--- | ---
268`alignCenter() => void` | Centers tiles horizontally within their parent container.
269
270### `MDCGridListAdapter`
271
272Method Signature | Description
273--- | ---
274`getOffsetWidth() => number` | Get root element `mdc-grid-list` offsetWidth.
275`getNumberOfTiles() => number` | Get the number of mdc-grid-tile elements contained within the grid list.
276`getOffsetWidthForTileAtIndex(index: number) => number` | Get offsetWidth of `mdc-grid-tile` at specified index.
277`setStyleForTilesElement(property: string, value: number) => void` | Set `mdc-grid-list__tiles` style property to provided value.
278`registerResizeHandler(handler: EventListener) => void` | Registers a handler to be called when the surface (or its viewport) resizes. Our default implementation adds the handler as a listener to the window's `resize()` event.
279`deregisterResizeHandler(handler: EventListener) => void` | Unregisters a handler to be called when the surface (or its viewport) resizes. Our default implementation removes the handler as a listener to the window's `resize()` event.