-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from privatenumber/body-class
feat: document.body and class support
- Loading branch information
Showing
13 changed files
with
417 additions
and
38 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 |
---|---|---|
@@ -1,4 +1,8 @@ | ||
const isProd = process.env.NODE_ENV === 'production'; | ||
|
||
module.exports = { | ||
presets: ['@babel/preset-env'], | ||
plugins: ['@babel/plugin-transform-runtime'], | ||
presets: [['@babel/preset-env', isProd ? {} : { | ||
useBuiltIns: 'usage', | ||
corejs: 2, | ||
}]], | ||
}; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,32 @@ | ||
// From https://github.com/vuejs/vue/blob/265dd457789bf726f95dd0578c1ce65a8b6ae9fd/src/platforms/web/runtime/class-util.js | ||
/* eslint-disable */ | ||
|
||
export function addClass(el, classes) { | ||
if (!classes.length) { | ||
return; | ||
} | ||
|
||
const { classList } = el; | ||
|
||
return classes.filter((c) => { | ||
if (!classList.contains(c)) { | ||
classList.add(c); | ||
return true; | ||
} | ||
}); | ||
} | ||
|
||
export function removeClass(el, classes) { | ||
if (!classes || !classes.length) { | ||
return; | ||
} | ||
|
||
let c; | ||
while (c = classes.shift()) { | ||
el.classList.remove(c); | ||
} | ||
|
||
if (!el.classList.length) { | ||
el.removeAttribute('class'); | ||
} | ||
}; |
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,29 @@ | ||
// From https://github.com/vuejs/vue/blob/531371b818b0e31a989a06df43789728f23dc4e8/src/platforms/web/util/class.js | ||
/* eslint-disable */ | ||
|
||
const isObject = (obj) => obj !== null && typeof obj === 'object'; | ||
|
||
const flatMap = (arr, f) => arr.reduce((acc, e) => acc.concat(f(e)), []); | ||
|
||
function stringifyObject(value) { | ||
let res = []; | ||
for (const key in value) { | ||
if (value[key]) { | ||
res.push(key); | ||
} | ||
} | ||
return res; | ||
} | ||
|
||
export function stringifyClass(value) { | ||
if (Array.isArray(value)) { | ||
return flatMap(value, v => stringifyClass(v)) | ||
} | ||
if (isObject(value)) { | ||
return stringifyObject(value); | ||
} | ||
if (typeof value === 'string' && value) { | ||
return [value]; | ||
} | ||
return []; | ||
}; |
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,14 @@ | ||
import { stringifyClass } from './class'; | ||
import { addClass, removeClass } from './class-util'; | ||
|
||
const BODY = typeof window !== 'undefined' && window.document.body; | ||
|
||
export default ({ data, parent }) => { | ||
const classStr = stringifyClass([data.staticClass, data.class]); | ||
if (!classStr) { return; } | ||
|
||
const added = addClass(BODY, classStr); | ||
const off = () => { removeClass(BODY, added); }; | ||
parent.$once('hook:beforeUpdate', off); | ||
parent.$once('hook:destroyed', off); | ||
}; |
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
Oops, something went wrong.