Skip to content

Commit

Permalink
fix: add img, span and div as elements with examples and visual…
Browse files Browse the repository at this point in the history
… test
  • Loading branch information
tujoworker committed Nov 9, 2020
1 parent aa2dc01 commit bf36cdd
Show file tree
Hide file tree
Showing 23 changed files with 523 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Strings where changed/removed and added to the [language files](/uilib/usage/cus
## New elements

- [list elements as React components](/uilib/elements/lists)`Li`, `Dt` and `Dd`. **Ol Lists** do now also support the `type` HTML attribute in order to use like _letters_ and _roman numerals_.
- [Image, span and div](/uilib/elements) elements to support more easily the [Skeleton](/uilib/components/skeleton) provider.

## New helper functions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,19 @@ import 'dnb-ui-lib/src/components/skeleton/style/themes/brand'

## Toggle skeleton on/off

<ComponentBox data-visual-test="skeleton-exclude" scope={{Provider}} useRender>
<ComponentBox data-visual-test="skeleton-exclude" useRender>
{`
const UserData = () => {
const [state, setState] = React.useState(true)
return (
<>
<Skeleton show={state}>
<H2 top bottom>Heading</H2>
<P top bottom>Paragraph Non habitasse ut nisi dictum laoreet ridiculus dui.</P>
<Input label_direction="vertical" label="Input" />
<Skeleton.Exclude>
<ToggleButton checked={state} on_change={({ checked }) => setState(checked)} top="large">Toggle</ToggleButton>
</Skeleton.Exclude>
</Skeleton>
</>
<Skeleton show={state}>
<H2 top bottom>Heading</H2>
<P top bottom>Paragraph Non habitasse ut nisi dictum laoreet ridiculus dui.</P>
<Input label_direction="vertical" label="Input" />
<Skeleton.Exclude>
<ToggleButton checked={state} on_change={({ checked }) => setState(checked)} top="large">Toggle</ToggleButton>
</Skeleton.Exclude>
</Skeleton>
)
}
render(<UserData />)
Expand Down
59 changes: 32 additions & 27 deletions packages/dnb-design-system-portal/src/docs/uilib/elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,35 @@ order: 6
import CodeBlock from 'Tags/CodeBlock'
import ComponentBox from 'Tags/ComponentBox'
import { Link } from 'dnb-ui-lib/src/elements'
import OtherElements from 'Pages/uilib/elements/other'
import NotSupportedElements from 'Pages/uilib/elements/not-supported'
import UnstyledElements from 'Pages/uilib/elements/unstyled'
import Anchor from 'Pages/uilib/elements/anchor'
import Blockquote from 'Pages/uilib/elements/blockquote'
import Tables from 'Pages/uilib/elements/tables'
import Lists from 'Pages/uilib/elements/lists'
import Image from 'Pages/uilib/elements/image'
import Hr from 'Pages/uilib/elements/horizontal-rule'
import Code from 'Pages/uilib/elements/code'

# HTML Elements

The `dnb-ui-lib` contains styling for the most commonly used [HTML Elements](https://developer.mozilla.org/en-US/docs/Web/HTML/Element) defined by the UX team at DNB. You may also have a look at [Typography](/uilib/typography) for headings and paragraph usage.

### How to use
## Overview

By default, no HTML element tag will be styled, except for [Headings and Paragraphs](/uilib/typography) which are styled by default without defining a class(es).
- [Anchor (Text Link)](#anchor-text-link)
- [Headings](/uilib/typography/heading)
- [Paragraph](/uilib/typography/paragraph)
- [Lists](#lists)
- [Tables](#tables)
- [Horizontal Rule](/uilib/elements#horizontal-rule)
- [Blockquote](#blockquote)
- [Image](#image)
- [Div/Span](#unstyled-html-elements)
- [Select](#missing-html-elements)
- [Code](#code)

### Vanilla HTML

In order to apply a style, you have to define a CSS class, like:

Expand All @@ -33,55 +48,45 @@ In order to apply a style, you have to define a CSS class, like:
`}
</CodeBlock>

Although, You have also the possibility to load a [sub package](/uilib/usage/customisation/styling#additional-packages), called **dnb-ui-tags** which applies all the styles to the HTML tags automatically. Use it with caution, as they can effect existing styles as well.

### React JSX

For those using JSX with React, you can simply use the wrapper Components.
For those using JSX with React, you can simply use the wrapper Components. They also inherit to the [Skeleton](/uilib/components/skeleton) provider.

```jsx
import { H1, H2, Lead, P, Link } from 'dnb-ui-lib/elements'
import { H1, H2, P, Anchor, Link } from 'dnb-ui-lib/elements'

render(
<article>
<H1>Semantic h1</H1>
<P>My Paragraph</P>
<Anchor href="/">Link</Anchor>
<Link href="/">Link</Link>
</article>
)
```

They work seamlessly with Styled Components as well:
#### Styled Components

They work seamlessly with Styled Components (emotion) as well:

<ComponentBox useRender scope={{Link}}>
{`
const StyledLink = styled(Link)\`
font-size: var(--font-size-basis);
font-weight: var(--font-weight-default);
color: var(--color-fire-red);
\`
render(<StyledLink href="/" target="_blank">Styled Link</StyledLink>)
`}
</ComponentBox>

## Overview

- [Anchor (Text Link)](#anchor-text-link)
- [Blockquote](#blockquote)
- [Lists](#lists)
- [Tables](#tables)
- [Select](#missing-html-elements)
- [Radio](#missing-html-elements)
- [Checkbox](#missing-html-elements)
- [Code](#code)

<Anchor />
<Blockquote />
<Lists />
<Tables />
<Blockquote />
<Image />
<Hr />
<UnstyledElements />
<Code />

---

<OtherElements />

---

<Code />
<NotSupportedElements />
111 changes: 111 additions & 0 deletions packages/dnb-design-system-portal/src/docs/uilib/elements/image.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
---
title: 'Image'
---

import ComponentBox from 'Tags/ComponentBox'

## Image

The image element associated with the class `dnb-img` does not have much opinionated styling. It exists more to have a future possibility to optimize and add features.

As of now, the React image "element" (Img) does provide a `figure` element with the `role="img"` and an `img` tag inside. This is mainly to support the [Skeleton](/uilib/components/skeleton) provider.

Styles wise, the background color is defined long with a centered alternative text.

```jsx
import { Img } from 'dnb-ui-lib/elements'

render(<Img alt="..." src="..." width="100" height="100" />)
```

### Basic image element

<ComponentBox data-visual-test="image-plain" useRender hideCode>
{`
const StyledImg = styled(Img)\`
border-radius: 1rem;
\`
const CustomImage = () => {
return (
<StyledImg
width="100"
height="100"
alt="DNB logo"
src="https://raw.githubusercontent.com/dnbexperience/eufemia/release/logo.png"
/>
)
}
render(<CustomImage />)
`}
</ComponentBox>

### Image with invalid source

<ComponentBox data-visual-test="image-no-source" useRender hideCode>
{`
const StyledImg = styled(Img)\`
border-radius: 1rem;
\`
const CustomImage = () => {
return (
<StyledImg
width="100"
height="100"
alt="Alt text"
src="https://invalid"
/>
)
}
render(<CustomImage />)
`}
</ComponentBox>

### Image with caption

<ComponentBox data-visual-test="image-caption" useRender hideCode>
{`
const StyledImg = styled(Img)\`
border-radius: 1rem;
\`
const CustomImage = () => {
return (
<StyledImg
width="100"
height="100"
alt="Alt text"
caption="Caption text"
src="https://raw.githubusercontent.com/dnbexperience/eufemia/release/logo.png"
/>
)
}
render(<CustomImage />)
`}
</ComponentBox>

### Image element with skeleton

<ComponentBox data-visual-test="image-skeleton" useRender>
{`
const StyledImg = styled(Img)\`
border-radius: 1rem;
\`
const CustomImage = () => {
const [state, setState] = React.useState(true)
return (
<Skeleton show={state}>
<StyledImg
width="100"
height="100"
alt="DNB logo"
src="https://raw.githubusercontent.com/dnbexperience/eufemia/release/logo.png"
/>
<br />
<Skeleton.Exclude>
<ToggleButton checked={state} on_change={({ checked }) => setState(checked)} top="large">Toggle</ToggleButton>
</Skeleton.Exclude>
</Skeleton>
)
}
render(<CustomImage />)
`}
</ComponentBox>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import CodeBlock from 'Tags/CodeBlock'

## Missing HTML Elements

Not every commonly used HTML Elements are included yet in the `dnb-ui-lib`. This decision is made by the DNB UX Team and relies on a principle to make UX design as good as possible, consistent and more thoughtful towards a broader customer target.

- For the `select` element, use the [**Dropdown**](/uilib/components/dropdown) component.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import ComponentBox from 'Tags/ComponentBox'

## Unstyled HTML Elements

In order to use the inherited [Skeleton](/uilib/components/skeleton), there are a number of un-styled HTML elements, that do inherit and react to the Skeleton Provider.

```jsx
import { Span, Div } from 'dnb-ui-lib/elements'
```

- `Span`
- `Div`

### Example usage of span

<ComponentBox data-visual-test="span-skeleton" useRender>
{`
const Box = styled.div\`
display: grid;
place-items: center;
width: 12rem;
height: 4rem;
padding: 0 1rem;
background-color: white;
\`
const StyledButton = styled.button\`
display: flex;
justify-content: space-between;
width: 100%;
&:hover {
color: var(--color-fire-red);
}
&:active {
opacity: 0.6;
}
\`
const CustomImage = () => {
const [state, setState] = React.useState(false)
return (
<Skeleton show={state}>
<Box>
<StyledButton className="dnb-button dnb-button--reset">
<Span>Text</Span>
<IconPrimary icon="chevron_right" />
</StyledButton>
</Box>
<br />
<Skeleton.Exclude>
<ToggleButton checked={state} on_change={({ checked }) => setState(checked)} top="large">Toggle</ToggleButton>
</Skeleton.Exclude>
</Skeleton>
)
}
render(<CustomImage />)
`}
</ComponentBox>
20 changes: 9 additions & 11 deletions packages/dnb-design-system-portal/src/shared/tags/Img.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@ const Img = ({
}
const props = { width, height }
return (
<React.Fragment>
<figure className={classnames('image-box', className)}>
<img
aria-hidden
alt={alt || caption}
src={src || children}
{...props}
/>
{caption && <figcaption>{caption}</figcaption>}
</figure>
</React.Fragment>
<figure className={classnames('image-box', className)}>
<img
aria-hidden
alt={alt || caption}
src={src || children}
{...props}
/>
{caption && <figcaption>{caption}</figcaption>}
</figure>
)
}

Expand Down
9 changes: 9 additions & 0 deletions packages/dnb-ui-lib/src/components/space/SpacingHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ export const findNearestTypes = (num) => {
export const isValidSpaceProp = (prop) =>
prop && ['top', 'right', 'bottom', 'left'].includes(prop)

export const removeSpaceProps = ({ ...props }) => {
for (let i in props) {
if (isValidSpaceProp(i)) {
delete props[i]
}
}
return props
}

// Creates a valid space CSS class out from given space types
export const createSpacingClasses = (props, Element = null) =>
Object.entries(props).reduce((acc, [direction, cur]) => {
Expand Down
19 changes: 19 additions & 0 deletions packages/dnb-ui-lib/src/elements/Div.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* HTML Element
*
*/

import React from 'react'
import PropTypes from 'prop-types'
import E from './Element'

const Div = (p) => <E is="div" skeleton_method="shape" {...p} />
Div.tagName = 'dnb-div'
Div.propTypes = {
children: PropTypes.node
}
Div.defaultProps = {
children: null
}

export default Div
Loading

0 comments on commit bf36cdd

Please sign in to comment.