-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
09d3710
commit 54e4825
Showing
2 changed files
with
135 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
packages/dnb-design-system-portal/src/docs/uilib/components/space/VisualHelpers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/** | ||
* Visual helper to show spacing | ||
* | ||
*/ | ||
|
||
import React, { useEffect, useState } from 'react' | ||
import PropTypes from 'prop-types' | ||
import styled from '@emotion/styled' | ||
import { Space } from 'dnb-ui-lib/src/components' | ||
|
||
const Block = styled.div` | ||
position: relative; | ||
display: flex; | ||
justify-content: center; | ||
width: 1.5rem; | ||
height: 1.5rem; | ||
background-color: var(--color-mint-green); | ||
` | ||
const Line = styled.div` | ||
position: absolute; | ||
bottom: 100%; | ||
display: flex; | ||
align-items: center; | ||
width: 0.0625rem; | ||
height: 100%; | ||
background-color: var(--color-cherry-red); | ||
${'' /* border-left: 0.0625rem dotted var(--color-cherry-red); */} | ||
` | ||
const MarginContainer = styled.div` | ||
position: relative; | ||
` | ||
const Margin = styled.div` | ||
position: absolute; | ||
bottom: 100%; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
width: 100%; | ||
height: 100%; | ||
background-color: rgba(213, 30, 149, 0.25); | ||
${'' /* border-left: 0.0625rem dotted var(--color-cherry-red); */} | ||
` | ||
const Label = styled.label` | ||
display: block; | ||
width: 1rem; | ||
margin-left: 0.25rem; | ||
font-size: 0.5rem; | ||
text-align: center; | ||
color: var(--color-black-80); | ||
` | ||
|
||
const MagicBox = ({ label, ...rest }) => { | ||
const ref = React.createRef() | ||
|
||
const [spaceInRem, setLabel] = useState(label) | ||
const [title, setTitle] = useState(null) | ||
|
||
if (!label) { | ||
useEffect(() => { | ||
const spaceInPixels = window | ||
.getComputedStyle(ref.current.parentElement) | ||
.getPropertyValue('margin-top') | ||
const spaceInRem = `${parseFloat(spaceInPixels) / 16}` | ||
setLabel(spaceInRem) | ||
|
||
const title = ref.current.parentElement.getAttribute('class') | ||
setTitle(title) | ||
}) | ||
} | ||
|
||
return ( | ||
<Block {...rest} ref={ref} title={title}> | ||
<Line style={{ height: `${spaceInRem}rem` }}></Line> | ||
<Label>{spaceInRem}</Label> | ||
</Block> | ||
) | ||
} | ||
MagicBox.propTypes = { | ||
label: PropTypes.string | ||
} | ||
MagicBox.defaultProps = { | ||
label: null | ||
} | ||
|
||
const VisualSpace = ({ label, children, ...rest }) => { | ||
const ref = React.createRef() | ||
|
||
const [spaceInRem, setLabel] = useState(label) | ||
const [title, setTitle] = useState(null) | ||
|
||
if (!label) { | ||
useEffect(() => { | ||
const spaceInPixels = window | ||
.getComputedStyle(ref.current.children[0]) | ||
.getPropertyValue('margin-top') | ||
const spaceInRem = `${parseFloat(spaceInPixels) / 16}` | ||
setLabel(spaceInRem) | ||
|
||
const title = ref.current.parentElement.getAttribute('class') | ||
setTitle(title) | ||
}) | ||
} | ||
|
||
return ( | ||
<Space {...rest} title={title}> | ||
<MarginContainer ref={ref}> | ||
{children} | ||
<Margin style={{ height: `${spaceInRem}rem` }}> | ||
<Label>{spaceInRem}</Label> | ||
</Margin> | ||
</MarginContainer> | ||
</Space> | ||
) | ||
} | ||
VisualSpace.propTypes = { | ||
label: PropTypes.string, | ||
children: PropTypes.node | ||
} | ||
VisualSpace.defaultProps = { | ||
label: null, | ||
children: null | ||
} | ||
|
||
export { MagicBox, VisualSpace } |