diff --git a/packages/dnb-design-system-portal/src/docs/uilib/components/space/Examples.js b/packages/dnb-design-system-portal/src/docs/uilib/components/space/Examples.js index 1525b76efeb..3075d5b1b2e 100644 --- a/packages/dnb-design-system-portal/src/docs/uilib/components/space/Examples.js +++ b/packages/dnb-design-system-portal/src/docs/uilib/components/space/Examples.js @@ -321,25 +321,31 @@ const VisualSpace = ({ label, children, ...rest }) => { React.useEffect(() => { if (!label) { - try { - const style = window.getComputedStyle(ref.current.children[0]) - const top = parseFloat(style.getPropertyValue('margin-top')) - const bottom = parseFloat(style.getPropertyValue('margin-bottom')) - let spaceInPixels = top - - if (bottom > 0) { - spaceInPixels = bottom - setDirection('bottom') + const elem = ref.current + const timeout = setTimeout(() => { + try { + const style = window.getComputedStyle(elem.children[0]) + const top = parseFloat(style.getPropertyValue('margin-top')) + const bottom = parseFloat( + style.getPropertyValue('margin-bottom') + ) + let spaceInPixels = top + + if (bottom > 0) { + spaceInPixels = bottom + setDirection('bottom') + } + + const spaceInRem = `${spaceInPixels / 16}` + setLabel(spaceInRem) + + const title = elem.parentElement.getAttribute('class') + setTitle(title) + } catch (e) { + console.warn(e) } - - const spaceInRem = `${spaceInPixels / 16}` - setLabel(spaceInRem) - - const title = ref.current.parentElement.getAttribute('class') - setTitle(title) - } catch (e) { - console.warn(e) - } + }, 1) + return () => clearTimeout(timeout) } }, [label, ref]) diff --git a/packages/dnb-design-system-portal/src/docs/uilib/components/space/info.md b/packages/dnb-design-system-portal/src/docs/uilib/components/space/info.md index 588b138352d..afc3e4cfcc0 100644 --- a/packages/dnb-design-system-portal/src/docs/uilib/components/space/info.md +++ b/packages/dnb-design-system-portal/src/docs/uilib/components/space/info.md @@ -31,12 +31,45 @@ To get a spacing of e.g. **2.5rem** (40px)- You may combine types `large` and `x ``` +With React, you can also use an object with the different directions: + +```jsx +/** All of these methods will result in the same spacing */ + +``` + ### Components and Spacing -Every component supports the spacing patterns, so it's possible to send in the `top`, `right`, `bottom` and `left` properties directly, like: +Every component supports the spacing patterns, so it's possible to send in the `top`, `right`, `bottom`, `left` and `space` properties directly, like: ```jsx