generated from nl-design-system/example
-
Notifications
You must be signed in to change notification settings - Fork 18
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
Showing
7 changed files
with
341 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<!-- @license CC0-1.0 --> | ||
|
||
# Flex wrap fallback | ||
|
||
The _flex wrap fallback_ component will choose between the default slot, and a fallback slot, based on flexbox wrapping. When the default content wraps over multiple lines, it will display the fallback content instead. | ||
|
||
For example: you can choose between a menu bar navigation for wide screens, and a hamburger menu for narrow screens. | ||
|
||
**Warning:** this component is experimental. |
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,5 @@ | ||
{ | ||
"utrecht": { | ||
"flex-wrap-fallback": {} | ||
} | ||
} |
155 changes: 155 additions & 0 deletions
155
packages/storybook-web-component/src/FlexWrapFallback.stories.tsx
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,155 @@ | ||
/* @license CC0-1.0 */ | ||
|
||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import readme from '@utrecht/components/flex-wrap-fallback/README.md?raw'; | ||
import tokensDefinition from '@utrecht/components/flex-wrap-fallback/tokens.json'; | ||
import tokens from '@utrecht/design-tokens/dist/index.json'; | ||
import { | ||
UtrechtButton, | ||
UtrechtButtonGroup, | ||
UtrechtDrawer, | ||
UtrechtFlexWrapFallback, | ||
UtrechtLink, | ||
} from '@utrecht/web-component-library-react'; | ||
import React, { PropsWithChildren, ReactNode } from 'react'; | ||
import { designTokenStory } from './design-token-story'; | ||
|
||
interface FlexWrapFallbackStoryProps { | ||
fallback?: ReactNode; | ||
} | ||
|
||
const FlexWrapFallbackStory = ({ children, fallback, ...restProps }: PropsWithChildren<FlexWrapFallbackStoryProps>) => ( | ||
<UtrechtFlexWrapFallback {...restProps}> | ||
{children} | ||
{fallback} | ||
</UtrechtFlexWrapFallback> | ||
); | ||
|
||
const meta = { | ||
title: 'Web Component/Flex-wrap fallback', | ||
id: 'web-component-flex-wrap-fallback', | ||
component: FlexWrapFallbackStory, | ||
argTypes: { | ||
children: { | ||
description: 'Content', | ||
}, | ||
fallback: { | ||
description: 'Fallback content', | ||
}, | ||
}, | ||
args: { | ||
children: [], | ||
}, | ||
tags: ['autodocs'], | ||
parameters: { | ||
status: { | ||
type: 'WORK IN PROGRESS', | ||
}, | ||
tokensPrefix: 'utrecht-flex-wrap-fallback', | ||
tokens, | ||
tokensDefinition, | ||
docs: { | ||
description: { | ||
component: readme, | ||
}, | ||
}, | ||
}, | ||
} satisfies Meta<typeof FlexWrapFallbackStory>; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Default: Story = { | ||
args: { | ||
children: [ | ||
<div | ||
style={{ | ||
display: 'flex', | ||
flexWrap: 'wrap', | ||
minHeight: '100px', | ||
justifyContent: 'space-between', | ||
inlineSize: '100%', | ||
}} | ||
> | ||
<div> | ||
<a href="https://example.com/">Home</a> | ||
</div> | ||
<div> | ||
<a href="https://example.com/2">Link 2</a> | ||
</div> | ||
<div> | ||
<a href="https://example.com/3">Link 3</a> | ||
</div> | ||
<div> | ||
<a href="https://example.com/4">Link 4</a> | ||
</div> | ||
<div> | ||
<a href="https://example.com/5">Link 5</a> | ||
</div> | ||
<div> | ||
<a href="https://example.com/6">Link 6</a> | ||
</div> | ||
<div> | ||
<a href="https://example.com/7">Link 7</a> | ||
</div> | ||
<div> | ||
<a href="https://example.com/8">Link 8</a> | ||
</div> | ||
<div> | ||
<a href="https://example.com/9">Link 9</a> | ||
</div> | ||
</div>, | ||
], | ||
fallback: [ | ||
<UtrechtButtonGroup slot="fallback"> | ||
<UtrechtButton appearance="subtle" expanded="false"> | ||
Open menu | ||
</UtrechtButton> | ||
</UtrechtButtonGroup>, | ||
<UtrechtDrawer open slot="fallback"> | ||
<ul> | ||
<li> | ||
<UtrechtLink href="https://example.com/">Home</UtrechtLink> | ||
</li> | ||
<li> | ||
<UtrechtLink href="https://example.com/2">Link 2</UtrechtLink> | ||
</li> | ||
<li> | ||
<UtrechtLink href="https://example.com/3">Link 3</UtrechtLink> | ||
</li> | ||
<li> | ||
<UtrechtLink href="https://example.com/4">Link 4</UtrechtLink> | ||
</li> | ||
<li> | ||
<UtrechtLink href="https://example.com/5">Link 5</UtrechtLink> | ||
</li> | ||
<li> | ||
<UtrechtLink href="https://example.com/6">Link 6</UtrechtLink> | ||
</li> | ||
<li> | ||
<UtrechtLink href="https://example.com/7">Link 7</UtrechtLink> | ||
</li> | ||
<li> | ||
<UtrechtLink href="https://example.com/8">Link 8</UtrechtLink> | ||
</li> | ||
<li> | ||
<UtrechtLink href="https://example.com/9">Link 9</UtrechtLink> | ||
</li> | ||
</ul> | ||
</UtrechtDrawer>, | ||
], | ||
}, | ||
decorators: [(Story) => <div style={{ inlineSize: '1280px', minBlockSize: '240px' }}>{Story()}</div>], | ||
name: 'No flex-wrap (1280px wide)', | ||
}; | ||
|
||
export const SmallInlineSize: Story = { | ||
args: { | ||
...Default.args, | ||
}, | ||
decorators: [(Story) => <div style={{ maxInlineSize: '320px', minBlockSize: '240px' }}>{Story()}</div>], | ||
name: 'Flex-wrap fallback (320px wide)', | ||
}; | ||
|
||
export const DesignTokens = designTokenStory(meta); |
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
24 changes: 24 additions & 0 deletions
24
packages/web-component-library-stencil/src/components/flex-wrap-fallback.scss
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,24 @@ | ||
/** | ||
* @license EUPL-1.2 | ||
* Copyright (c) 2020-2022 Gemeente Utrecht | ||
* Copyright (c) 2020-2022 Frameless B.V. | ||
*/ | ||
|
||
:host { | ||
display: block; | ||
} | ||
|
||
:host([hidden]) { | ||
display: none !important; | ||
} | ||
|
||
.utrecht-flex-wrap-fallback__content--hidden, | ||
.utrecht-flex-wrap-fallback__fallback--hidden { | ||
block-size: 0; | ||
opacity: 0%; | ||
outline: 0; | ||
overflow: hidden; | ||
pointer-events: none; | ||
position: relative; | ||
user-select: none; | ||
} |
83 changes: 83 additions & 0 deletions
83
packages/web-component-library-stencil/src/components/flex-wrap-fallback.tsx
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,83 @@ | ||
/** | ||
* @license EUPL-1.2 | ||
* Copyright (c) 2020-2022 Gemeente Utrecht | ||
* Copyright (c) 2020-2022 Frameless B.V. | ||
*/ | ||
|
||
import { Component, Element, h, Prop, State } from '@stencil/core'; | ||
|
||
/** | ||
* Checks if an element has `flex-wrap: wrap` and the content wraps over multiple lines | ||
*/ | ||
const hasFlexboxWrap = (el: Element) => { | ||
const win = el?.ownerDocument?.defaultView; | ||
|
||
const style = win?.getComputedStyle(el); | ||
|
||
if (style && style.getPropertyValue('flex-wrap') === 'wrap' && style.getPropertyValue('display') === 'flex') { | ||
// TODO: Account for writing-mode vertical | ||
return el.firstElementChild.getBoundingClientRect().top !== el.lastElementChild.getBoundingClientRect().top; | ||
} else { | ||
return false; | ||
} | ||
}; | ||
|
||
@Component({ | ||
tag: 'utrecht-flex-wrap-fallback', | ||
styleUrl: 'flex-wrap-fallback.scss', | ||
shadow: true, | ||
}) | ||
export class FlexWrapFallback { | ||
@Element() hostElement: HTMLElement; | ||
|
||
/* ID reference for element with `display: flex;` or `display: inline-flex` and `flex-wrap: wrap` */ | ||
@Prop({ attribute: 'flextarget', reflect: true }) flexTarget?: string; | ||
@State() resizeObserver: ResizeObserver; | ||
@State() contentWraps: boolean; | ||
|
||
connectedCallback() { | ||
if (!this.resizeObserver) { | ||
this.resizeObserver = new ResizeObserver(() => { | ||
// TODO: Support updating `flextarget` attribute, switch ResizeObserver to match | ||
// TODO: Escape ID selector CSS query | ||
const cssSelector = this.flexTarget ? `#${this.flexTarget}` : ':not([slot])'; | ||
const flexTargetEl = this.hostElement.querySelector(cssSelector); | ||
console.log(cssSelector, flexTargetEl); | ||
this.contentWraps = !!flexTargetEl && hasFlexboxWrap(flexTargetEl); | ||
}); | ||
} | ||
|
||
this.resizeObserver.observe(this.hostElement); | ||
} | ||
|
||
disconnectedCallback() { | ||
this.resizeObserver?.unobserve(this.hostElement); | ||
} | ||
render() { | ||
const { contentWraps } = this; | ||
return ( | ||
<div> | ||
<div | ||
id="wrap" | ||
class={`utrecht-flex-wrap-fallback__content ${ | ||
contentWraps ? 'utrecht-flex-wrap-fallback__content--hidden' : '' | ||
}`} | ||
data-inert={contentWraps} | ||
aria-hidden={contentWraps} | ||
> | ||
<slot></slot> | ||
</div> | ||
<div | ||
id="nowrap" | ||
class={`utrecht-flex-wrap-fallback__fallback ${ | ||
contentWraps ? '' : 'utrecht-flex-wrap-fallback__fallback--hidden' | ||
}`} | ||
data-inert={!contentWraps} | ||
hidden={!contentWraps} | ||
> | ||
<slot name="fallback"></slot> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} |
Oops, something went wrong.