-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(components): post-header #3837
Merged
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
bb39644
first draft
gfellerph f6fe97a
Merge branch 'main' into header
gfellerph ff103c9
feat(components): added a provisional post-header component
gfellerph 723299c
feat(styles): enabled media mixins with string params
gfellerph 8555de5
Merge branch 'main' into header
gfellerph e3387a6
remove duplicate selector
gfellerph cd5683f
fix linting
gfellerph 65cbcfa
Merge branch 'main' into header
gfellerph e645b58
rename resize function
gfellerph aa76316
reset body background color
gfellerph 362fbb0
feat(components): Main navigation and megadropdown (#3846)
gfellerph 7ff809d
Merge branch 'main' into header
gfellerph 972e41e
recompile
gfellerph File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,7 @@ | ||
--- | ||
'@swisspost/design-system-components': minor | ||
'@swisspost/design-system-components-angular': minor | ||
'@swisspost/design-system-components-react': minor | ||
--- | ||
|
||
Added a provisional post-header component with some basic functionality in place. This component is not finished in this state. |
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
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
137 changes: 137 additions & 0 deletions
137
packages/components/src/components/post-header/post-header.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,137 @@ | ||
@use '@swisspost/design-system-styles/mixins/media'; | ||
|
||
*, | ||
::before, | ||
::after { | ||
box-sizing: border-box; | ||
} | ||
|
||
:host { | ||
--global-header-height: 72px; | ||
--main-header-height: 56px; | ||
--header-height: calc(var(--global-header-height) + var(--main-header-height)); | ||
|
||
@include media.max(lg) { | ||
--global-header-height: 64px; | ||
} | ||
} | ||
|
||
.d-flex { | ||
display: flex; | ||
} | ||
|
||
.space-between { | ||
justify-content: space-between; | ||
} | ||
|
||
.global-header { | ||
background-color: #ffcc00; | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
position: sticky; | ||
padding-inline-start: 4px; | ||
padding-inline-end: 12px; | ||
|
||
height: var(--global-header-height); | ||
|
||
@include media.max(lg) { | ||
top: 0; | ||
} | ||
|
||
@include media.min(lg) { | ||
top: calc((var(--global-header-height) - 24px) * -1); | ||
} | ||
} | ||
|
||
slot[name='post-logo'] { | ||
align-self: flex-end; | ||
} | ||
|
||
.global-sub { | ||
display: flex; | ||
align-items: center; | ||
gap: 2rem; | ||
height: var(--global-header-height); | ||
} | ||
|
||
.align-end { | ||
align-items: flex-end; | ||
} | ||
|
||
.logo { | ||
flex: 1 0 auto; | ||
height: var(--global-header-height); | ||
width: var(--global-header-height); | ||
min-height: 24px; | ||
align-self: flex-end; | ||
|
||
@include media.min(lg) { | ||
height: calc(var(--global-header-height) - var(--header-scroll-top)); | ||
} | ||
} | ||
|
||
::slotted(ul), | ||
::slotted(post-mainnavigation) { | ||
margin-block: 0; | ||
list-style: none; | ||
display: flex; | ||
padding-left: 0; | ||
gap: 1rem; | ||
} | ||
|
||
.title-header, | ||
.main-navigation { | ||
display: flex; | ||
align-items: center; | ||
padding-inline: 12px; | ||
background: white; | ||
} | ||
|
||
.title-header { | ||
height: var(--main-header-height); | ||
display: flex; | ||
align-items: center; | ||
|
||
@include media.max(lg) { | ||
border-bottom: 1px solid black; | ||
} | ||
} | ||
:host(:not(:has([slot='title']))) .title-header { | ||
display: none; | ||
} | ||
|
||
::slotted(h1) { | ||
margin: 0 !important; | ||
font-size: 28px !important; | ||
} | ||
|
||
.main-navigation { | ||
position: sticky; | ||
top: 24px; | ||
height: var(--main-header-height); | ||
|
||
@include media.min(lg) { | ||
border-bottom: 1px solid black; | ||
} | ||
|
||
@include media.max(lg) { | ||
display: none; | ||
position: absolute; | ||
top: var(--header-height); | ||
bottom: 0; | ||
width: 100%; | ||
background-color: white; | ||
height: auto; | ||
|
||
&.extended { | ||
display: block; | ||
} | ||
} | ||
} | ||
|
||
.mobile-toggle { | ||
@include media.min(lg) { | ||
display: none; | ||
} | ||
} |
127 changes: 127 additions & 0 deletions
127
packages/components/src/components/post-header/post-header.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,127 @@ | ||
import { Component, h, Host, State, Element } from '@stencil/core'; | ||
import { throttle } from 'throttle-debounce'; | ||
import { version } from '@root/package.json'; | ||
|
||
@Component({ | ||
tag: 'post-header', | ||
shadow: true, | ||
styleUrl: './post-header.scss', | ||
}) | ||
export class PostHeader { | ||
@Element() host: HTMLPostHeaderElement; | ||
@State() device: 'mobile' | 'tablet' | 'desktop' = null; | ||
@State() mobileMenuExtended: boolean = false; | ||
|
||
private scrollParent = null; | ||
private throttledScroll = () => this.handleScrollEvent(); | ||
private debouncedResize = throttle(50, () => this.handleResize()); | ||
|
||
componentWillRender() { | ||
this.scrollParent = this.getScrollParent(this.host); | ||
this.scrollParent.addEventListener('scroll', this.throttledScroll, { passive: true }); | ||
window.addEventListener('resize', this.debouncedResize, { passive: true }); | ||
this.handleResize(); | ||
this.handleScrollEvent(); | ||
} | ||
|
||
private handleScrollEvent() { | ||
// Credits: "https://github.com/qeremy/so/blob/master/so.dom.js#L426" | ||
const st = Math.max( | ||
0, | ||
this.scrollParent instanceof Document | ||
? this.scrollParent.documentElement.scrollTop | ||
: this.scrollParent.scrollTop, | ||
); | ||
|
||
this.host.style.setProperty('--header-scroll-top', `${st}px`); | ||
} | ||
|
||
private getScrollParent(node: Element): Element | Document { | ||
let currentParent = node.parentElement; | ||
while (currentParent) { | ||
if (currentParent.nodeName === 'BODY') { | ||
return document; | ||
} | ||
if (this.isScrollable(currentParent)) { | ||
return currentParent; | ||
} | ||
currentParent = currentParent.parentElement; | ||
} | ||
return document; | ||
} | ||
|
||
private isScrollable(node: Element) { | ||
if (!(node instanceof HTMLElement || node instanceof SVGElement)) { | ||
return false; | ||
} | ||
const style = getComputedStyle(node); | ||
return ['overflow', 'overflow-x', 'overflow-y'].some(propertyName => { | ||
const value = style.getPropertyValue(propertyName); | ||
return value === 'auto' || value === 'scroll'; | ||
}); | ||
} | ||
|
||
private handleResize() { | ||
const width = window?.innerWidth; | ||
if (width >= 1024) { | ||
this.device = 'desktop'; | ||
} else if (width >= 600) { | ||
this.device = 'tablet'; | ||
} else { | ||
this.device = 'mobile'; | ||
} | ||
} | ||
|
||
private handleMobileMenuToggle() { | ||
this.mobileMenuExtended = !this.mobileMenuExtended; | ||
} | ||
|
||
render() { | ||
const mainNavClasses = ['main-navigation']; | ||
if (this.mobileMenuExtended) { | ||
mainNavClasses.push('extended'); | ||
} | ||
|
||
return ( | ||
<Host version={version}> | ||
<div class="global-header"> | ||
<div class="global-sub"> | ||
<div class="logo"> | ||
<slot name="post-logo"></slot> | ||
</div> | ||
{this.device === 'desktop' && <slot name="audience-navigation"></slot>} | ||
</div> | ||
<div class="global-sub"> | ||
{this.device === 'desktop' && <slot name="meta-navigation"></slot>} | ||
<slot name="global-controls"></slot> | ||
{this.device === 'desktop' && <slot name="post-language-switch"></slot>} | ||
<div onClick={() => this.handleMobileMenuToggle()} class="mobile-toggle"> | ||
<slot name="post-togglebutton"></slot> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div class="title-header d-flex space-between align-center"> | ||
<slot name="title"></slot> | ||
<div class="global-sub"> | ||
<slot name="local-controls"></slot> | ||
<slot></slot> | ||
</div> | ||
</div> | ||
|
||
<div class={mainNavClasses.join(' ')}> | ||
{(this.device === 'mobile' || this.device === 'tablet') && ( | ||
<slot name="audience-navigation"></slot> | ||
)} | ||
<slot name="post-mainnavigation"></slot> | ||
{(this.device === 'mobile' || this.device === 'tablet') && ( | ||
<slot name="meta-navigation"></slot> | ||
)} | ||
{(this.device === 'mobile' || this.device === 'tablet') && ( | ||
<slot name="post-language-switch"></slot> | ||
)} | ||
</div> | ||
</Host> | ||
); | ||
} | ||
} |
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,10 @@ | ||
# post-header | ||
|
||
|
||
|
||
<!-- Auto Generated Below --> | ||
|
||
|
||
---------------------------------------------- | ||
|
||
*Built with [StencilJS](https://stenciljs.com/)* |
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 |
---|---|---|
|
@@ -14,4 +14,3 @@ | |
.description { | ||
@include utilities.visuallyhidden; | ||
} | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There seem to be a mismatch with the
throttled/debounced
naming.