Skip to content
SiteEmail

Relative

The Relative component is a structural element designed to offset content or act as a bounding box for Absolute components. By applying offsets using the top, left, right, and bottom props, the Relative component allows precise positioning of nested content relative to its original position.

The primary use case for the Relative component is to offset nested content within the document structure or serve as a container for Absolute components.

import Relative from '@components/Layout/Relative/Relative';
import Block from '@components/Layout/Block/Block';
const App = () => {
return (
<Relative top="50px" right="50px">
<Block>Content with top and right offset by 50px</Block>
</Relative>
);
};
export default App;
Prop NameTypeDefaultDescription
styleJSX.CSSProperties{}Inline styles to apply directly to the component’s root element.
classstring""Additional CSS classes to apply to the component
refHTMLDivElementundefinedRetrieves the component’s DOM element and assigns it to a variable.
top, left, right, bottomstring""Specify the offset of the Relative component. It accepts any valid css unit value
onActionRecord<string, (scope?: string, ...args: any[]) => void>undefinedAllows you to add custom navigation action handlers to the component. See the Navigation component documentation for details.
anchorstring | HTMLElementundefinedLinks navigation to another element. When the anchor element is focused, this component’s actions will execute. Can be a CSS selector or HTMLElement.

The Relative component’s position is controlled by its position props ( top, left, right, bottom ). Under the hood the component uses CSS position to offset the element. You can pass any valid CSS unit (e.g., px, %, vh, em) to these props to specify the directional offset of the component.

import Relative from '@components/Layout/Relative/Relative';
import Block from '@components/Layout/Block/Block';
const App = () => {
return (
<Relative top="15vh" right="200px">
<Block>Content with top offset of 15vh and right offset by 50px</Block>
</Relative>
);
};
export default App;

Important: Since the Relative component uses position: relative, the offset applied is purely visual. The component remains part of the document flow, meaning that any subsequent content will still be positioned as if the Relative component occupies its original space.

If you need to remove content from the document flow entirely (e.g., for tooltips, modals, or overlays), consider using the Absolute Component, which positions elements relative to their closest positioned ancestor or the viewport.

One of the main purposes of the Relative component is to act as the bounding box for nested Absolute components. When an Absolute component is used within a Relative component, its position is calculated relative to the Relative component’s boundaries. This relationship ensures precise control over the placement of the Absolute component without affecting other elements on the page.

import Relative from '@components/Layout/Relative/Relative';
import Absolute from '@components/Layout/Absolute/Absolute';
import Block from '@components/Layout/Block/Block';
const App = () => {
return (
<Relative style={{ width: '300px', height: '200px', border: '1px solid black' }}>
<Block>Normally positioned content</Block>
<Absolute top="20px" left="40px">
This content is positioned 20px from the top and 40px from the left of the Relative container.
</Absolute>
<Absolute bottom="0" right="0">
Positioned at the bottom right corner of the Relative container.
</Absolute>
</Relative>
);
};
export default App;

To access the HTML DOM element of the Relative component.

  1. Declare a variable to hold the ref but don’t initialize it with a value
  2. The declared value should have a type of HTMLDivElement.
  3. Set the declared variable as the value of the ref prop of the Relative component
import Relative from '@components/Layout/Relative/Relative';
import Block from '@components/Layout/Block/Block';
let relativeRef!: HTMLDivElement
const App = () => {
return (
<Relative ref={relativeRef} top="50px" right="50px">
<Block>Content with top and right offset by 50px</Block>
</Relative>
);
};
export default App;

Now you can access the HTML element of Relative with relativeRef and make modifications to it if needed.