import * as React from 'react'; import { CodeSnippet } from '@fluentui/docs-components'; import { Button, Box, Flex, Header, Segment, Divider } from '@fluentui/react-northstar'; import ExampleSnippet from '../components/ExampleSnippet'; import DocPage from '../components/DocPage'; import GuidesNavigationFooter from '../components/GuidesNavigationFooter'; import { code, link } from '../utils/helpers'; import { EmojiIcon } from '@fluentui/react-icons-northstar'; const links = { flex: link('Flex', '/components/flex'), flexItem: link('Flex.Item', '/components/flex'), grid: link('Grid', '/components/grid'), segment: link('Segment', '/components/segment'), box: link('Box', '/components/segment'), }; export default () => (
Overview

The following components are introduced by Fluent UI to handle layout aspects:

Flex vs Grid

Sometimes it might not be evident if some particular case is a case of 1D (i.e. {code('Flex')}) or 2D (i.e.{' '} {code('Grid')}) layout. As a result, quite often {code('Grid')} component is used at places where it is sufficient to use {code('Flex')}.

Here is the question you might ask yourself to decide on that:

Note on Segment and Box components

{links.segment} and {links.box} components may be abused for layout purposes, like in the following examples:

Segment's misuse: handle opaque background cases
`} /> { const OpaqueBackground = ({ children, color }) => ( ); OpaqueBackground.displayName = 'OpaqueBackground'; return This is a bad approach to opaque background :(; }} />
Box's misuse: handle relative positioning cases
`} /> { const RelativePositioned = ({ children, left }) => ( ); RelativePositioned.displayName = 'RelativePositioned'; return ( This is a bad way to support relative-positioning :( ); }} />

While it might seem that the intent is addressed with the approach taken, however, this is wrong for the following reason:

Fluent UI makes it very important to follow component's semantics at the first place, and only then consider the visual aspects.

If we'd refer to the semantics provided in the description for {links.segment} component:

A segment is used to create a grouping of related content.

This description suggests that purpose of {code('Segment')} is not a layout. Thus, this component shouldn't be used for layout purposes, as this will break component's semantics - and, as a consequence, it might break accessibility and theming, as those both adhere to component's semantics at the first place.

Same conclusions applies to the {links.box} component.

Flex recipies
There are several advices that might help when using {code('Flex')} component.
{code('Flex.Item')} should be used sparingly
Use {code('Flex.Item')} component to wrap child elements only in case if their flex styles should be overriden.

There is no strict need to use {code('Flex.Item')} component as a direct child of {code('Flex')} - one may use{' '} {code('Flex.Item')} component only when it is necessary to tweak flex styles of individual child item.

(