Resumen de la tarjeta React
La tarjeta Ignite UI for React muestra texto, imágenes, iconos y botones en una presentación visualmente rica que puede servir como punto de entrada a información más detallada. Las tarjetas se pueden utilizar para crear un panel de control multimedia.
React Card Example
Usage
Las tarjetas le permiten mostrar fácilmente contenido compuesto por diferentes tipos de objetos u objetos similares cuyo tamaño y acciones admitidas pueden variar.
Empezando
Primero, debes instalar el paquete npm Ignite UI for React correspondiente ejecutando el siguiente comando:
npm install igniteui-react
Entonces tendrás que importar elIgrCard CSS necesario y su contenido, así:
import { IgrCard, IgrCardHeader, IgrCardContent, IgrCardMedia, IgrCardActions } from 'igniteui-react';
import 'igniteui-webcomponents/themes/light/bootstrap.css';
Para una introducción completa al Ignite UI for React, lee el tema Empezar.
Luego, para representar la plantilla de la tarjeta de demostración, podemos agregar el siguiente código:
<IgrCard>
<IgrCardMedia>
<img src="https://images.unsplash.com/photo-1518235506717-e1ed3306a89b?ixlib=rb-1.2.1&auto=format&fit=crop&w=640&q=50"></img>
</IgrCardMedia>
<IgrCardHeader>
<h3 slot="title">New York City</h3>
<h5 slot="subtitle">City in New York</h5>
</IgrCardHeader>
<IgrCardContent>
<p>New York City comprises 5 boroughs sitting where the
Hudson River meets the Atlantic Ocean. At its core is Manhattan,
a densely populated borough that’s among the world’s major commercial,
financial and cultural centers.</p>
</IgrCardContent>
<IgrCardActions>
<IgrButton>
<span>Read more</span>
<IgrRipple />
</IgrButton>
<div slot="end">
<IgrIconButton name="twitter" collection="material">
<IgrRipple />
</IgrIconButton>
<IgrIconButton name="facebook" collection="material">
<IgrRipple />
</IgrIconButton>
</div>
</IgrCardActions>
</IgrCard>
You will notice a few things above. First, when we want to tag an element as a header title, like the h3 heading, we place it between the IgrCardHeader tags and set its slot name to title. Conversely, if we wanted to make another heading element a subtitle we would name its slot subtitle.
Any image or video we want to show in the card, we wrap inside the IgrCardMedia tags. The IgrCardMedia allows us to size the content placed inside so that it maintains its aspect ratio while filling the element’s entire content box. If the object's aspect ratio does not match the aspect ratio of its box, then the object will be clipped to fit.
You can place anything inside the IgrCardContent tags. Usually text goes there.
Finally, the IgrCardActions is where you'd place any actionable items, like buttons.
Media, Thumbs, and Avatars
If you want to show an image or icon in the card header next to the title and subtitle, you can do it by assigning the element's slot property to thumbnail.
Taking the card above as an example, we can edit the contents of the IgrCardHeader and add an avatar with slot="thumbnail":
<IgrCardHeader>
<div slot="thumbnail">
<IgrAvatar src="path/to/image" initials="TS" />
</div>
<h3 slot="title">Title</h3>
<h5 slot="subtitle">Subtitle</h5>
</IgrCardHeader>
El ejemplo anterior mostrará el avatar junto al título y subtítulo en el encabezado de la tarjeta.
Outlined cards
The card has an outlined attribute which, if set, removes any shadows from the card, replacing them with a thin border to separate the card from the background.
Horizontal Layout
Por defecto, todas las secciones de la tarjeta (encabezado, contenido, medios, acciones) están dispuestas verticalmente. Esto es agradable cuando tenemos mucho espacio vertical. Imagina que queremos disponer las secciones de la tarjeta horizontalmente. Podemos lograr un diseño así con un simple CSS.
A continuación se muestra un ejemplo de una tarjeta horizontal delineada:
<IgrCard>
<div className="card-horizontal">
<div>
<IgrCardHeader>
<img src="https://static.infragistics.com/xplatform/images/music/rozes.jpg" slot="thumbnail"></img>
<h5 slot="title">Rozes</h5>
<h5 slot="subtitle">Under the Grave (2016)</h5>
</IgrCardHeader>
<IgrCardContent>
<p>As I have always said: I write what’s real and what’s true,
even if it means throwing myself under the bus.</p>
</IgrCardContent>
</div>
<div className="divider"></div>
<IgrCardActions>
<span className="material-icons">skip_previous</span>
<span className="material-icons">play_arrow</span>
<span className="material-icons">skip_next</span>
</IgrCardActions>
</div>
</IgrCard>
We are using an additional div element to bundle the IgrCardHeader and IgrCardContent together, keeping them aligned vertically, and applying the .card-horizontal class to the wrapping div element to align the two sections of the card horizontally.
Los estilos que aplica esa.card-horizontal clase son:
.card-horizontal {
display: flex;
flex-direction: row;
flex: 1 1 0%;
}
.card-horizontal img {
width: 64px;
height: 64px;
}
.card-horizontal igc-card-actions {
justify-content: center;
}
Si todo ha ido bien nuestra tarjeta debería quedar así:
Alternative layouts
You can get even more creative with the layout of the IgrCard.
Below is an example showing how you can create a semi-horizontal card, where we have every section of the card laid out vertically, while the IgrCardMedia appears alongside the vertical sections.
<IgrCard>
<div className="semi-horizontal">
<div>
<IgrCardHeader>
<IgrAvatar src="https://static.infragistics.com/xplatform/images/music/singer_with_mic.jpg" slot="thumbnail" />
<h5 slot="title">HERE</h5>
<h5 slot="subtitle">by Mellow D</h5>
</IgrCardHeader>
<IgrCardContent>
<p>Far far away, behind the word mountains,
far from the countries Vokalia and Consonantia,
there live the blind texts.</p>
</IgrCardContent>
<IgrCardActions>
<IgrButton><span>Play Album</span></IgrButton>
</IgrCardActions>
</div>
<IgrCardMedia className='card-media'>
<img src="https://static.infragistics.com/xplatform/images/music/singer_female.jpg"></img>
</IgrCardMedia>
</div>
</IgrCard>
.semi-horizontal {
display: flex;
flex-direction: row;
flex-grow: 1;
}
.card-media {
width: 96px;
min-width: 96px;
}
Card Actions
El área de acciones de la tarjeta permite una configuración adicional a lo que ya hemos comentado.
Puede invertir el orden del botón de texto y los botones de ícono cambiando los nombres de las ranuras.
<IgrCardActions>
<IgrButton>
<span>Read more</span>
<IgrRipple />
</IgrButton>
<div slot="start">
<IgrIconButton className="marginIcon" name="twitter" collection="material">
<IgrRipple />
</IgrIconButton>
<IgrIconButton name="facebook" collection="material">
<IgrRipple />
</IgrIconButton>
</div>
</IgrCardActions>
Ahora los botones de iconos aparecerán antes del botón de texto.
También puede agregar más contenido en el medio simplemente omitiendo la propiedad de la ranura y dejando que los elementos vayan a la ranura predeterminada.
Styling
Since the card is a container that wraps different elements, styling is done by styling its building blocks - the IgrCardHeader, IgrCardContent, IgrCardMedia and IgrCardActions sub-components.
igc-card {
background-color: var(--ig-secondary-900);
}
igc-card-content,
igc-card-header::part(title) {
color: var(--ig-primary-500-contrast);
}
igc-card-header > *[slot="subtitle"] {
color: var(--ig-warn-500);
opacity: 0.9;
}
igc-icon-button::part(base) {
background-color: var(--ig-primary-300);
}
Summary
En este artículo cubrimos mucho terreno con el componente de la tarjeta. Creamos una tarjeta simple y agregamos algunas imágenes para hacerla un poco más atractiva. Utilizamos algunos React adicionales dentro de nuestra tarjeta, como avatares, botones e iconos, para enriquecer la experiencia y agregar algunas funcionalidades. Y finalmente, cambiamos la apariencia de la carta cambiando los colores principales de los bloques de construcción.
API References
IgrAvatarIgrButtonIgrCardActionsIgrCardContentIgrCardHeaderIgrCardMediaIgrCardIgrIconButtonIgrIconStyling & Themes