-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate @hadl/patternlab-plugin-pattern-wrap into core (#1433)
* feat: integrate @hadl/patternlab-plugin-pattern-wrap into core * test: pattern wrap classes reading from markdown and/or json Refs: #1432 * docs: pattern wrap classes config and details documentation Refs: #1432 Co-authored-by: Josef Bredreck <[email protected]>
- Loading branch information
1 parent
c32a45c
commit 414e038
Showing
10 changed files
with
240 additions
and
2 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* get the classes from pattern markdown and/or json | ||
* @param {PatternLab} patternlab | ||
* @param {Pattern} pattern | ||
* @return {string} | ||
*/ | ||
function getPatternWrapClasses(patternlab, pattern) { | ||
const { patternWrapClassesEnable, patternWrapClassesKey } = patternlab.config; | ||
if ( | ||
!patternWrapClassesEnable || | ||
!patternWrapClassesKey || | ||
patternWrapClassesKey.length === 0 | ||
) { | ||
return ''; | ||
} | ||
|
||
const classes = []; | ||
patternWrapClassesKey.forEach((key) => { | ||
const { allMarkdown, jsonFileData } = pattern; | ||
|
||
if (allMarkdown && allMarkdown[key]) { | ||
classes.push(allMarkdown[key]); | ||
} | ||
|
||
if (jsonFileData && jsonFileData[key]) { | ||
classes.push(jsonFileData[key]); | ||
} | ||
}); | ||
|
||
return classes.join(' '); | ||
} | ||
|
||
/** | ||
* change pattern template and wrap with classes pattern wrapper | ||
* @param {PatternLab} patternlab | ||
* @param {Pattern} pattern | ||
*/ | ||
function patternWrapClassesChangePatternTemplate(patternlab, pattern) { | ||
const classes = getPatternWrapClasses(patternlab, pattern); | ||
if (classes.length !== 0) { | ||
pattern.patternPartialCode = `<div class="pl-pattern-wrapper-element ${classes}">${pattern.patternPartialCode}</div>`; | ||
} | ||
} | ||
|
||
module.exports = patternWrapClassesChangePatternTemplate; |
3 changes: 3 additions & 0 deletions
3
packages/core/test/files/_patterns/test/pattern-wrap-class-json.json
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,3 @@ | ||
{ | ||
"theme-class": "json-theme-class" | ||
} |
1 change: 1 addition & 0 deletions
1
packages/core/test/files/_patterns/test/pattern-wrap-class-json.mustache
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 @@ | ||
bar |
3 changes: 3 additions & 0 deletions
3
packages/core/test/files/_patterns/test/pattern-wrap-class-markdown.md
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,3 @@ | ||
--- | ||
theme-class: markdown-theme-class | ||
--- |
1 change: 1 addition & 0 deletions
1
packages/core/test/files/_patterns/test/pattern-wrap-class-markdown.mustache
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 @@ | ||
bar |
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,53 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const tap = require('tap'); | ||
|
||
const loadPattern = require('../src/lib/loadPattern'); | ||
const patternWrapClassesChangePatternTemplate = require('../src/lib/patternWrapClasses'); | ||
const util = require('./util/test_utils.js'); | ||
const patternEngines = require('../src/lib/pattern_engines'); | ||
const config = require('./util/patternlab-config.json'); | ||
|
||
patternEngines.loadAllEngines(config); | ||
|
||
const patterns_dir = `${__dirname}/files/_patterns`; | ||
|
||
tap.test('reading pattern wrap class from markdown', function (test) { | ||
const patternlab = util.fakePatternLab(patterns_dir); | ||
patternlab.config = { | ||
...patternlab.config, | ||
patternWrapClassesEnable: true, | ||
patternWrapClassesKey: ['theme-class'], | ||
}; | ||
|
||
const patternPathMarkdown = path.join( | ||
'test', | ||
'pattern-wrap-class-markdown.mustache' | ||
); | ||
const patternMarkdown = loadPattern(patternPathMarkdown, patternlab); | ||
patternWrapClassesChangePatternTemplate(patternlab, patternMarkdown); | ||
const patternPartialMarkdown = | ||
'<div class="pl-pattern-wrapper-element markdown-theme-class"></div>'; | ||
|
||
test.equal(patternMarkdown.patternPartialCode, patternPartialMarkdown); | ||
test.end(); | ||
}); | ||
|
||
tap.test('reading pattern wrap class from json', function (test) { | ||
const patternlab = util.fakePatternLab(patterns_dir); | ||
patternlab.config = { | ||
...patternlab.config, | ||
patternWrapClassesEnable: true, | ||
patternWrapClassesKey: ['theme-class'], | ||
}; | ||
|
||
const patternPathJson = path.join('test', 'pattern-wrap-class-json.mustache'); | ||
const patternJson = loadPattern(patternPathJson, patternlab); | ||
patternWrapClassesChangePatternTemplate(patternlab, patternJson); | ||
const patternPartialJson = | ||
'<div class="pl-pattern-wrapper-element json-theme-class"></div>'; | ||
|
||
test.equal(patternJson.patternPartialCode, patternPartialJson); | ||
test.end(); | ||
}); |
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
105 changes: 105 additions & 0 deletions
105
packages/docs/src/docs/advanced-pattern-wrap-classes.md
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,105 @@ | ||
--- | ||
title: Pattern Wrap Classes | ||
tags: | ||
- docs | ||
category: advanced | ||
eleventyNavigation: | ||
title: Pattern Wrap Classes | ||
key: advanced | ||
order: 300 | ||
sitemapPriority: '0.8' | ||
--- | ||
|
||
This feature allows you to add a wrapper div with css class(es) around a pattern when shown in the single preview. | ||
If it gets included in another pattern, the wrapper is not added. | ||
|
||
This comes in handy if you, for example, use theming classes to visualize different backgrounds, colors etc. | ||
|
||
## Configuration | ||
|
||
Enable this feature with the configuration options | ||
[patternWrapClassesEnable](/docs/editing-the-configuration-options/#heading-patternwrapclassesenable) and | ||
[patternWrapClassesKey](/docs/editing-the-configuration-options/#heading-patternwrapclasseskey). | ||
|
||
## How does it work? | ||
|
||
Patternlab will look for any "data key" added to the `patternWrapClassesKey` array and adds that | ||
date to the wrapper element classes. | ||
|
||
Data key can be set inside the Markdown or JSON file of any pattern. | ||
|
||
### Example Config | ||
|
||
```json | ||
"patternWrapClassesKey": ["theme-class"] | ||
``` | ||
|
||
## Use in Markdown | ||
|
||
Usage [Documenting Patterns](/docs/documenting-patterns/) | ||
|
||
### my-pattern.md | ||
```markdown | ||
--- | ||
theme-class: my-theme-class | ||
--- | ||
``` | ||
|
||
### Result | ||
```html | ||
<div class="pl-pattern-wrapper-element my-theme-class">...markup of pattern...</div> | ||
``` | ||
|
||
## Use in JSON | ||
|
||
Usage [Creating Pattern-specific Values](/docs/creating-pattern-specific-values/) | ||
|
||
### my-pattern.json | ||
```json | ||
{ | ||
"theme-class": "my-other-theme-class" | ||
} | ||
``` | ||
|
||
### Result | ||
```html | ||
<div class="pl-pattern-wrapper-element my-other-theme-class">...markup of pattern...</div> | ||
``` | ||
|
||
## Pseudo-Patterns | ||
|
||
This will work with pseudo-patterns too ([Using Pseudo-Patterns](/docs/using-pseudo-patterns/)) | ||
|
||
### my-pattern~variant.json | ||
```json | ||
{ | ||
"theme-class": "my-variant-theme-class" | ||
} | ||
``` | ||
|
||
### Result | ||
```html | ||
<div class="pl-pattern-wrapper-element my-variant-theme-class">...markup of pattern...</div> | ||
``` | ||
|
||
## Multiple entries in "patternWrapClassesKey" | ||
|
||
Will result in multiple classes in the wrapper div. | ||
|
||
### Example Config | ||
```json | ||
"patternWrapClassesKey": ["theme-class", "other-class"] | ||
``` | ||
|
||
### my-pattern.json | ||
```json | ||
{ | ||
"theme-class": "theme-class", | ||
"other-class": "some-other-class" | ||
} | ||
``` | ||
|
||
### Result | ||
```html | ||
<div class="pl-pattern-wrapper-element theme-class some-other-class">...markup of pattern...</div> | ||
``` |