Skip to content
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: add default and overscroll backgrounds #3945

Open
wants to merge 28 commits into
base: 3432-helpers-palettes
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
c40bfb5
Create a map with the core tokens.
alizedebray Oct 23, 2024
caeb101
Create accent styles.
alizedebray Oct 23, 2024
9e1a091
Create accent docs.
alizedebray Oct 23, 2024
df0163c
Fix body color issue.
alizedebray Oct 23, 2024
43aab6c
Fix background not applied.
alizedebray Oct 23, 2024
c1ff36d
Temporary tokens.
alizedebray Oct 23, 2024
869c34c
Allow for multi layer tokens.
alizedebray Oct 23, 2024
0fc90d7
Add cargo theme.
alizedebray Oct 23, 2024
cc16e38
Improve styles and docs
alizedebray Oct 24, 2024
b6e6e0d
Rename "accent" to "palette"
alizedebray Oct 29, 2024
913ee67
Merge branch 'main' into accents
alizedebray Oct 29, 2024
d135614
Update StylesSwitcher.tsx
alizedebray Oct 29, 2024
55b2ba8
Add tests
alizedebray Oct 29, 2024
6d141fc
Add changeset
alizedebray Oct 29, 2024
46740c0
Merge branch 'main' into 3432-helpers-palettes
alizedebray Nov 4, 2024
994c909
Update tokens
alizedebray Nov 4, 2024
340ce84
Update tokens build
alizedebray Nov 4, 2024
ea27fdd
Merge branch 'main' into 3432-helpers-palettes
alizedebray Nov 5, 2024
ad9d391
fix code smells
alizedebray Nov 5, 2024
1b6e9bc
Merge branch '3432-helpers-palettes' of https://github.com/swisspost/…
alizedebray Nov 5, 2024
49a123e
Merge branch 'main' into 3432-helpers-palettes
alizedebray Nov 5, 2024
3fa6537
update with new tokens
alizedebray Nov 5, 2024
51d48ab
Update build-tokens.yaml
alizedebray Nov 5, 2024
e6482ce
Merge branch 'main' into 3432-helpers-palettes
alizedebray Nov 7, 2024
3055a0a
Update palette tokens build
alizedebray Nov 7, 2024
f86e684
feat: add overscroll and default body background color
gfellerph Nov 8, 2024
94bc582
Update packages/styles/src/elements/body.scss
gfellerph Nov 8, 2024
809d706
Update packages/styles/src/elements/body.scss
gfellerph Nov 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/weak-jars-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@swisspost/design-system-documentation': minor
'@swisspost/design-system-styles': minor
---

Added color palettes to easily apply colors to a page section using predefined color sets.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useState } from 'react';
import { IconButton, WithTooltip } from '@storybook/components';

const THEMES = ['Post'] as const;
const THEMES = ['Post', 'Cargo'] as const;
const CHANNELS = ['External', 'Internal'] as const;
const SCHEMES = ['Light', 'Dark'] as const;

Expand All @@ -15,18 +15,6 @@ const possibleStylesheets = THEMES.flatMap(theme => {
return CHANNELS.map(channel => getStylesheetUrl(theme, channel));
});

/*
* Backgrounds
*/
const backgroundClasses: { [key in (typeof SCHEMES)[number]]: string } = {
Light: 'bg-white',
Dark: 'bg-dark',
};
const getBackgroundClass = (scheme: string) => {
return scheme in backgroundClasses ? backgroundClasses[scheme] : '';
};
const possibleBackgrounds = SCHEMES.map(scheme => getBackgroundClass(scheme));

/*
* Local storage access
*/
Expand Down Expand Up @@ -113,9 +101,8 @@ function StylesSwitcher() {
if (!stories) return;

stories.forEach(story => {
story.classList.remove(...possibleBackgrounds);
story.classList.add(getBackgroundClass(currentScheme));
story.setAttribute('data-color-scheme', currentScheme.toLowerCase());
if (!story.classList.contains('palette-default')) story.classList.add('palette-default');
});
}, [stories, currentScheme]);

Expand Down
2 changes: 2 additions & 0 deletions packages/documentation/.storybook/styles/preview.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#storybook-root,
#storybook-docs {
background-color: white;

.sbdocs-content .container >,
.sbdocs-content .container > post-tabs > post-tab-panel >,
.sbdocs-content .container > migration-global-state {
Expand Down
13 changes: 13 additions & 0 deletions packages/documentation/cypress/e2e/components/palette.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
describe('Palette', () => {
describe('Accessibility', () => {
beforeEach(() => {
cy.visit('/iframe.html?id=snapshots--palette');
cy.get('.palette-default', { timeout: 30000 }).should('be.visible');
cy.injectAxe();
});

it('Has no detectable a11y violations on load for all variants', () => {
cy.checkA11y('#root-inner');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe('Palette', () => {
it('default', () => {
cy.visit('/iframe.html?id=snapshots--palette');
cy.get('.palette-default', { timeout: 30000 }).should('be.visible');
cy.percySnapshot('Palettes', { widths: [1440] });
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe('Accent', () => {
it('accent', () => {
cy.visit('/iframe.html?id=snapshots--accent');
cy.get('.accent-default', { timeout: 30000 }).should('be.visible');
cy.percySnapshot('Accents', { widths: [1440] });
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Canvas, Controls, Meta } from '@storybook/blocks';
import * as paletteStories from './palette.stories';

<Meta of={paletteStories} />

<div className="docs-title">
# Palettes
</div>

<div className="lead">
Easily apply colors to a section of your page using predefined color sets.
</div>

There are four different palettes:
- **Default** - The standard palette used when no other palette is specified.
- **Alternate** - Used to differentiate alternating sections from the body, without strong emphasis or highlighting.
- **Accent** - A complementary color used for highlights and emphasis.
- **Brand** - The primary color for the brand.

Palettes may also include a specific text color that is different from the body color.
You can apply this text color to any element by using the `.palette-text` class.

<Canvas sourceState="shown" of={paletteStories.Default} />

<div className="hide-col-default">
<Controls of={paletteStories.Default} />
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { StoryObj } from '@storybook/web-components';
import { html } from 'lit';
import meta from './palette.stories';

const { id, ...metaWithoutId } = meta;

export default {
...metaWithoutId,
title: 'Snapshots',
};

type Story = StoryObj;

export const Palette: Story = {
render: () => {
return html`${['light', 'dark'].map(
scheme => html`
<div class="palette-default p-24" data-color-scheme=${scheme}>
${meta.argTypes.palette.options.map(palette => meta.render({ palette }))}
</div>
`,
)}`;
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Args, Meta, StoryObj } from '@storybook/web-components';
import { html } from 'lit';

const meta: Meta = {
id: '43481535-5b39-40b5-a273-478b07dc3b31',
title: 'Foundations/Palettes',
tags: ['package:HTML'],
render: renderPalette,
parameters: {
palettes: [],
design: {
type: 'figma',
url: 'https://www.figma.com/file/xZ0IW0MJO0vnFicmrHiKaY/Components-Post?type=design&node-id=18172-73431&mode=design&t=3lniLiZhl7q9Gqgn-4',
},
},
args: {
palette: 'default',
},
argTypes: {
palette: {
name: 'Palette',
description: 'The set of colors used for a section of the page.',
control: {
type: 'radio',
labels: {
default: 'Default',
alternate: 'Alternate',
brand: 'Brand',
accent: 'Accent',
},
},
options: ['default', 'alternate', 'accent', 'brand'],
table: {
category: 'General',
},
},
},
};

export default meta;

// RENDERER
function renderPalette(args: Args) {
return html`
<div class="palette-${args.palette} p-24">
<p>I use the main body color.</p>
<p style="palette-text">
I use a specific color from the palette (it might be the same as the body color).
</p>
</div>
`;
}

// STORIES
type Story = StoryObj;

export const Default: Story = {};
2 changes: 1 addition & 1 deletion packages/styles/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ gulp.task('copy', () => {
* See https://github.com/pnpm/pnpm/issues/8338 for more information and reproduction
*/
gulp.task('temporarily-copy-token-files', () => {
return gulp.src(['../tokens/dist/*.scss']).pipe(gulp.dest('./src/tokens/temp'));
return gulp.src(['../tokens/dist/**/*.scss']).pipe(gulp.dest('./src/tokens/temp'));
});

/**
Expand Down
8 changes: 8 additions & 0 deletions packages/styles/src/cargo-external.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@use './tokens/schemes';
@use './tokens/device';
@use './tokens/external';
@use './tokens/cargo-theme';
@use 'palettes/cargo-palettes';
@use 'utilities/index';
@use './elements';
@use './components';
8 changes: 8 additions & 0 deletions packages/styles/src/cargo-internal.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@use './tokens/schemes';
@use './tokens/device';
@use './tokens/internal';
@use './tokens/cargo-theme';
@use 'palettes/cargo-palettes';
@use 'utilities/index';
@use './elements';
@use './components';
8 changes: 8 additions & 0 deletions packages/styles/src/cargo-tokens-external.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@use 'sass:meta';

@layer design-system {
@include meta.load-css('./tokens/schemes');
@include meta.load-css('./tokens/device');
@include meta.load-css('./tokens/external');
@include meta.load-css('./tokens/cargo-theme');
}
8 changes: 8 additions & 0 deletions packages/styles/src/cargo-tokens-internal.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@use 'sass:meta';

@layer design-system {
@include meta.load-css('./tokens/schemes');
@include meta.load-css('./tokens/device');
@include meta.load-css('./tokens/internal');
@include meta.load-css('./tokens/cargo-theme');
}
10 changes: 10 additions & 0 deletions packages/styles/src/elements/body.scss
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
@use 'sass:map';
@use '../variables/color';
@use '../functions/tokens';
@use '../tokens/elements';
@use '../tokens/palettes';
gfellerph marked this conversation as resolved.
Show resolved Hide resolved

tokens.$default-map: elements.$post-body;

// Resets type
@use '../components/fonts';

html {
// This in combination with the default background color on the body creates a nice effect when overscrolling
// in Safari and Chrome on a Mac because the header and the footer are yellow, the yellow surface stretches
// beyond the body. If this is not yellow, overscrolling reveals a white background and the header
// suddenly seems in the middle of the page.
background-color: color.$yellow;
}

body {
font-family: tokens.get('body-font-family');
font-size: tokens.get('body-font-size');
Expand Down
56 changes: 56 additions & 0 deletions packages/styles/src/palettes/_mixins.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
@use 'sass:map';
@use 'sass:meta';

@use '../functions/contrast';
@use '../placeholders/schemes';

@use '../functions/tokens';
@use '../tokens/elements';
@use '../tokens/palettes';

@use './variables' as *;

@mixin generate-palette-class($name, $theme) {
.palette-#{$name} {
&,
&:is([data-color-scheme='light'], [data-color-scheme='light'] *) {
@include palette-styles($name, $theme, 'light', $redefine-tokens: true);
}

&:is([data-color-scheme='dark'], [data-color-scheme='dark'] *) {
@include palette-styles($name, $theme, 'dark', $redefine-tokens: true);
}
}
}

@mixin default-palette-styles($theme) {
&:not([data-color-scheme='dark']) {
@include palette-styles($default-palette, $theme, 'light');
}

&[data-color-scheme='dark'] {
@include palette-styles($default-palette, $theme, 'dark');
}
}

@mixin palette-styles($name, $theme, $scheme, $redefine-tokens: false) {
$palette: map.get(meta.module-variables(palettes), '#{$theme}-#{$scheme}');

@if ($palette) {
--post-palette-fg: #{tokens.get('palettes-color-#{$name}-fg', palettes.$post-palettes)};
--post-palette-bg: #{tokens.get('palettes-color-#{$name}-bg', palettes.$post-palettes)};
background-color: var(--post-palette-bg) !important;

@if ($redefine-tokens == true) {
// redefining the body color is required so that the new color scheme is taken into account
color: tokens.get('body-color', elements.$post-body);

$bg-scheme: tokens.get('helper-color-#{$name}-bg-scheme', $palette);
@if ($bg-scheme == 'light') {
@extend %color-scheme-light;
} @else {
@extend %color-scheme-dark;
}
}
}
}
2 changes: 2 additions & 0 deletions packages/styles/src/palettes/_variables.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$default-palette: 'default';
$palettes: $default-palette, 'alternate', 'brand', 'accent';
10 changes: 10 additions & 0 deletions packages/styles/src/palettes/cargo-palettes.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@use 'mixins' as *;
@use 'variables' as *;

body {
@include default-palette-styles('cargo');
}

@each $palette in $palettes {
@include generate-palette-class($palette, 'cargo');
}
10 changes: 10 additions & 0 deletions packages/styles/src/palettes/post-palettes.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@use 'mixins' as *;
@use 'variables' as *;

body {
@include default-palette-styles('post');
}

@each $palette in $palettes {
@include generate-palette-class($palette, 'post');
}
8 changes: 6 additions & 2 deletions packages/styles/src/post-external.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
@use './post-tokens-external';
@use './utilities';
@use './tokens/schemes';
@use './tokens/device';
@use './tokens/external';
@use './tokens/post-theme';
@use 'palettes/post-palettes';
@use 'utilities/index';
@use './elements';
@use './components';
8 changes: 6 additions & 2 deletions packages/styles/src/post-internal.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
@use './post-tokens-internal';
@use './utilities';
@use './tokens/schemes';
@use './tokens/device';
@use './tokens/internal';
@use './tokens/post-theme';
@use 'palettes/post-palettes';
@use 'utilities/index';
@use './elements';
@use './components';
8 changes: 8 additions & 0 deletions packages/styles/src/tokens/_cargo-theme.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@use './temp/theme' as themes;

@use './core';
@use './../mixins/tokens';

:root {
@include tokens.from(themes.$post-cargo);
}
5 changes: 5 additions & 0 deletions packages/styles/src/tokens/_palettes.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@use './temp/palettes/post-light' as post-light;

@forward './temp/palettes';

$post-light: post-light.$post-palettes;
5 changes: 5 additions & 0 deletions packages/styles/src/utilities/_variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -251,5 +251,10 @@ $utilities: (
values: inline inline-block block grid inline-grid table table-row table-cell flex inline-flex
none,
),
'accent-text': (
css-var: true,
css-variable-name: accent-fg,
values: null,
),
// IMPORTANT: When adding new utilities here, please ensure to remove the corresponding bootstrap utilities in `src/themes/bootstrap/_utilities.scss`.
);
1 change: 1 addition & 0 deletions packages/tokens/_build/configs/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import './all.js';
import './palettes.js';
import './tailwind.js';
Loading
Loading