Skip to content

Commit 2bda9ee

Browse files
feat(styles): add initial entrypoints and tests for styles (#8400)
* feat(styles): add updated motion package, docs, and tests * chore(styles): add tests for colors * feat(styles): add additional flags for config and add tests * chore(styles): add snapshots * docs(style): add testing style guide and recipes * feat(styles): add additional entrypoints and tests * docs(style): update README and package.json with description * fix(styles): update entrypoint * chore(grid): update tests for grid Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent fe420aa commit 2bda9ee

34 files changed

+1201
-23
lines changed

docs/migration/11.x-motion.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44

55
## Changes
66

7-
| v10 | v11 |
8-
| -------------------------- | ---------- |
9-
| `$carbon--easings` | `$easings` |
10-
| `@function carbon--motion` | `motion` |
11-
| `@mixin carbon--motion` | `motion` |
7+
| v10 | v11 |
8+
| -------------------------- | ----------------------- |
9+
| `$carbon--easings` | `$easings` |
10+
| `@function carbon--motion` | `@function motion` |
11+
| `@mixin carbon--motion` | `@mixin motion` |
12+
| | `$duration-fast-01` |
13+
| | `$duration-fast-02` |
14+
| | `$duration-moderate-01` |
15+
| | `$duration-moderate-02` |
16+
| | `$duration-slow-01` |
17+
| | `$duration-slow-02` |

docs/style.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,3 +729,73 @@ When writing SassDoc comments, you should use three forward slashes:
729729
// ...
730730
}
731731
```
732+
733+
### Testing
734+
735+
We use the `@carbon/test-utils` package to test our Sass styles in JavaScript.
736+
Inside of this package, there is a `SassRenderer` module that you can bring in
737+
that allows you to get values from Sass in JavaScript to be used in test
738+
assertions.
739+
740+
The basic template for tests for Sass files will look like:
741+
742+
```js
743+
/**
744+
* <COPYRIGHT>
745+
*
746+
* @jest-environment node
747+
*/
748+
749+
'use strict';
750+
751+
const { SassRenderer } = require('@carbon/test-utils/scss');
752+
753+
const { render } = SassRenderer.create(__dirname);
754+
755+
describe('@carbon/styles/scss/config', () => {
756+
test('Public API', async () => {
757+
const { get } = await render(`
758+
// You can bring in modules using the path from the test file
759+
@use '../path/to/sass/module';
760+
761+
$test: true;
762+
763+
// The `get` helper will let you pass a value from Sass to JavaScript
764+
$_: get('test', $test);
765+
`);
766+
767+
// get('<key>') gives you both the JavaScript representation of a value
768+
// along with the `nativeValue` which comes from Dart sass. Use `.value`
769+
// to get the JavaScript value and make assertions
770+
expect(get('test').value).toBe(true);
771+
});
772+
});
773+
```
774+
775+
#### Recipes
776+
777+
##### Public API
778+
779+
Sometimes it is useful to assert that a module's Public API matches what is
780+
expected or does not change between versions. To do this in a test file, you can
781+
use the `sass:meta` module along with several helpers for getting the variables
782+
and functions from a module. Unfortunately, mixins need to be checked by hand
783+
using the `mixin-exists` function from `sass:meta`.
784+
785+
```js
786+
test('Public API', async () => {
787+
await render(`
788+
@use 'sass:meta';
789+
@use '../path/to/module';
790+
791+
// Get the variables for the module under the namespace `module`
792+
$_: get('variables', meta.module-variables('module'));
793+
794+
// Get the functions for the module under the namespace `module`
795+
$_: get('variables', meta.module-functions('module'));
796+
797+
// Verify that a mixin exists, optionally within a module
798+
$_: get('mixin-name', meta.mixin-exists('mixin-name', 'module');
799+
`);
800+
});
801+
```

packages/grid/__tests__/scss-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('@carbon/grid', () => {
2121
2222
$_: get('variables', meta.module-variables('grid'));
2323
$_: get('mixins', (
24-
grid: meta.mixin-exists('grid', 'grid'),
24+
grid: meta.mixin-exists('css-grid', 'grid'),
2525
));
2626
`);
2727

packages/grid/index.scss

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,4 @@
99
$prefix: 'bx' !default,
1010
);
1111
@forward 'scss/modules/breakpoint';
12-
@forward 'scss/modules/mixins';
13-
14-
@use 'scss/modules/mixins';
15-
16-
@include mixins.grid();
12+
@forward 'scss/modules/css-grid';

packages/motion/index.scss

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,33 @@ $easings: (
5454
@mixin motion($name, $mode) {
5555
transition-timing-function: motion($name, $mode);
5656
}
57+
58+
/// @access public
59+
/// @type Duration
60+
/// @group @carbon/motion
61+
$duration-fast-01: 70ms;
62+
63+
/// @access public
64+
/// @type Duration
65+
/// @group @carbon/motion
66+
$duration-fast-02: 110ms;
67+
68+
/// @access public
69+
/// @type Duration
70+
/// @group @carbon/motion
71+
$duration-moderate-01: 150ms;
72+
73+
/// @access public
74+
/// @type Duration
75+
/// @group @carbon/motion
76+
$duration-moderate-02: 240ms;
77+
78+
/// @access public
79+
/// @type Duration
80+
/// @group @carbon/motion
81+
$duration-slow-01: 400ms;
82+
83+
/// @access public
84+
/// @type Duration
85+
/// @group @carbon/motion
86+
$duration-slow-02: 700ms;

packages/styles/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# @carbon/styles
2+
3+
> Styles for the Carbon Design System
4+
5+
## Getting started
6+
7+
To install `@carbon/styles` in your project, you will need to run the following
8+
command using [npm](https://www.npmjs.com/):
9+
10+
```bash
11+
npm install -S @carbon/styles
12+
```
13+
14+
If you prefer [Yarn](https://yarnpkg.com/en/), use the following command
15+
instead:
16+
17+
```bash
18+
yarn add @carbon/styles
19+
```
20+
21+
## Usage
22+
23+
### Sass entrypoints
24+
25+
| Entrypoint | Description |
26+
| ---------------------------- | ------------------------------------------------------------------------------------------------------------ |
27+
| `index.scss` | Entrypoint that brings in all of the CSS for the Carbon Design System |
28+
| `scss/_breakpoint.scss` | Use helpers to easily create breakpoints in your product that align to the 16 column grid |
29+
| `scss/_colors.scss` | Provides access to colors from the IBM Design Language |
30+
| `scss/_config.scss` | Allows you to configure various behaviors of Carbon |
31+
| `scss/_feature-flags.scss` | Enable or disable various feature flags to try out experiments within Carbon |
32+
| `scss/_grid.scss` | Provides access to grid mixins and helpers for working with the 16 column grid |
33+
| `scss/_motion.scss` | Provides access to motion easing curves and durations from the IBM Design Language |
34+
| `scss/_reset.scss` | Brings in a CSS reset to enable consistent styles across browsers |
35+
| `scss/_themes.scss` | Provides access to the default themes used with Carbon |
36+
| `scss/_theme.scss` | Provides access to the current theme and theme helpers to use in your project |
37+
| `scss/_type.scss` | Provides access to type tokens configured for use with IBM Plex |
38+
| `scss/components/*` | Bring in a single component, or several that you need for your project. Great for optimizing CSS bundle size |
39+
| `scss/components/index.scss` | Bring in all component styles |
40+
41+
### Optimizing CSS bundle size
42+
43+
When you bring in `@carbon/styles` directly, it will include all of the styles
44+
available for the Carbon Design System. If you would like to bring in only the
45+
styles that you use, then we recommend structuring your imports in the following
46+
way:
47+
48+
```scss
49+
// Bring in top-level / global styles
50+
@use '@carbon/styles/reset';
51+
52+
// Bring in each component that you use
53+
@use '@carbon/styles/scss/components/<component>';
54+
```
55+
56+
For example, if your project is only using the Accordion and Breadcrumb
57+
components then you would do the following:
58+
59+
```scss
60+
@use '@carbon/styles/reset';
61+
@use '@carbon/styles/scss/components/accordion';
62+
@use '@carbon/styles/scss/components/breadcrumb';
63+
```
64+
65+
## 🙌 Contributing
66+
67+
We're always looking for contributors to help us fix bugs, build new features,
68+
or help us improve the project documentation. If you're interested, definitely
69+
check out our [Contributing Guide](/.github/CONTRIBUTING.md)! 👀
70+
71+
## 📝 License
72+
73+
Licensed under the [Apache 2.0 License](/LICENSE).

packages/styles/index.scss

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
@forward 'scss/config' with (
99
$prefix: 'bx' !default,
1010
);
11-
@forward '@carbon/colors';
12-
@forward '@carbon/grid' hide $prefix;
13-
@forward '@carbon/layout' hide map-deep-get, key-by-index, last-map-item;
14-
@forward '@carbon/motion';
15-
@forward '@carbon/themes' hide $white;
16-
@forward '@carbon/type' hide $prefix;
11+
@forward 'scss/colors' hide $white;
12+
@forward 'scss/grid';
13+
@forward 'scss/motion';
14+
@forward 'scss/type';
15+
@forward 'scss/themes';
16+
@forward 'scss/theme';
1717

1818
@use 'scss/reset';
1919
@use 'scss/components';

packages/styles/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "@carbon/styles",
33
"private": true,
4+
"description": "Styles for the Carbon Design System",
45
"version": "0.4.0",
56
"license": "Apache-2.0",
67
"repository": {
@@ -18,6 +19,7 @@
1819
],
1920
"dependencies": {
2021
"@carbon/colors": "^10.23.0",
22+
"@carbon/feature-flags": "^0.3.0",
2123
"@carbon/grid": "^10.25.0",
2224
"@carbon/layout": "^10.23.0",
2325
"@carbon/motion": "^10.16.0",

0 commit comments

Comments
 (0)