On this page
Import
import { Graphic } from '@envsemble/envui';
Usage
A graphic takes a file in src, such as an imported PNG, WebP, JPEG or SVG, or an SVG component as its children, and alt, which TypeScript requires.
import logo from './logo.svg';
<Graphic src={logo} alt="Northwind" size="md" />
<Button startIcon={<Graphic src={logo} alt="" />}>Sign in with Northwind</Button>
<Graphic alt="" tint="text">
<PipelineIcon />
</Graphic>
size="icon", the default, reads--env-icon-size, which an icon slot sets, else--env-control-icon-size;textsits a little below the middle of the line, as an icon in text does.size="fill"is a block as wide as its container, and as tall where the container has a definite height; otherwise animgtakes its own shape's height, and a file in the text color is square.aspect, such as"16/9", sets the box'saspect-ratio: a step is then its height, and a fill takes its height from its width, so it needs no height from its container.fit="cover"covers the box and crops a file's picture, for a photo or a screenshot.- The picture is an
imgwithobject-fit: contain, orcover, or the SVG at 100% of the box, in a grid of fixed tracks, so its size never comes from the file. An SVG component needs aviewBox; itswidthandheightdon't count. tint="text"paints an SVG component's filled and stroked shapes incurrentColor, and a file through a CSS mask of its shape. A file from another origin needs CORS for the mask.
Trim a file once
npx envui-graphic <file> crops a PNG, WebP or JPEG to its visible content, its transparent or one-color border, and fits an SVG's viewBox to its drawing. It writes the trimmed file next to the original, or into --out <dir>, and prints the Graphic to use. It reads the files with sharp, which envUI doesn't install: add it with npm install --save-dev sharp.
npx envui-graphic src/assets/logo.png
Composition
Every icon slot, the env-icon wrapper of Button, IconButton, Menu, navigation, Tabs, Badge, fields and list rows, sizes and centers any child, an img, an svg or a Graphic, to its --env-icon-size. List.Item's and Combobox's start give an application's own svg or img the icon size too, and List.Item's an envUI symbol as well. TopBar's and PageHeader's start set a small and a medium picture's size, for a Graphic of the icon size and an svg or img alike, while an svg that follows the text, such as an envUI symbol, keeps its size.
Customize
--env-picture-size-sm, -md, -lg and -xl hold the sizes from sm to xl, which Avatar and AvatarGroup take too, so they stay the same. The lint rule no-raw-graphics reports a raw img, an svg with a fixed size, another icon package, a Graphic without alt and a margin on one.
Words in code
| Words | In code |
|---|---|
| Our logo | Graphic with the logo file in src and the company's name in alt |
| Our own icon | Graphic with the icon's SVG component as children, in an icon slot |
| A photo or a thumbnail | size="fill" or a step, with aspect="16/9" and fit="cover" |
| As large as an avatar | size="sm", "md", "lg" or "xl" |
| In the text color | tint="text" |
| Trim the logo | npx envui-graphic <file> |
Accessibility
You provide
alt: what the picture says, such as the company's name, or""when the text beside it says it already.- Contrast for a picture that carries meaning: its own colors aren't checked against the theme;
tint="text"takes the text color's.
envUI provides
- A graphic with
altnamed as an image by it, withrole="img", whether from a file or an SVG component. - A graphic with an empty
althidden from assistive technology, as decoration. - In a button, a menu or navigation, the icon slot hides it, since the label names the action.
Props
Graphic
An application's own logo, icon or picture, from a file or an SVG component, at one of envUI's sizes: contained and centered in its box, or covering it for a photo, never stretched, and never at the file's own size.
| Prop | Type | Description |
|---|---|---|
src | stringundefined | The file's URL, such as an imported PNG, WebP, JPEG or SVG. While it is undefined, such as before a chosen file is read, the box stays empty at its size. |
children | ReactNode | An SVG, such as the application's own icon component, with a viewBox. |
altrequired | string | What the picture says, such as the company's name for its logo: it names the graphic as an image. `""` when the text beside it already says it, such as a logo before the name: the graphic is decorative and hidden from assistive technology. |
sizeDefault 'icon' | 'text''icon''sm''md''lg''xl''fill' | The box the picture is contained and centered in: `text` 1 em, in a line of text; `icon` the icon size of the slot it sits in, such as a button's, else a control's 1 rem; `sm` 1.5 rem, `md` 2 rem, `lg` 3 rem and `xl` 5.5 rem, Avatar's sizes; `fill` a block of its container's width, and of its height where the container has a definite one. Its size never comes from the file. |
aspect | stringnumber | The box's shape as a ratio of width to height, such as `"16/9"` or `1.5`, for a photo, a screenshot or a thumbnail: a step's size is then its height, and a fill takes its height from its width, so it needs no height from its container. Without it the box is square, or a fill the container's box. |
fitDefault 'contain' | 'contain''cover' | `contain` shows the whole picture inside the box; `cover` fills the box and crops what overflows, for a photo or a screenshot. It applies to a file; an SVG component is contained. |
tintDefault 'own' | 'own''text' | `own` keeps the picture's colors; `text` paints a one-color picture in the text color, so it follows the theme and the state of the control it sits in. A file in the text color is drawn through a CSS mask, which needs a file from another origin to be served with CORS. |
Also accepts the native props of <span>, such as ref, className and aria-* attributes.
Part names
Each part's class and state attributes are public and versioned, for an application's CSS to style.
env-graphic- The box at the graphic's size, which contains and centers the picture, or which it covers.
env-graphic-image- A file's picture in its own colors.
env-graphic-mask- A file's shape painted in the text colour.
data-aspect- Present
data-fitcoverdata-sizefill·icon·lg·md·sm·text·xldata-tinttext
Changelog
- 1.0.0
Graphic, the application's own logos, icons and pictures at envUI's sizes.