Skip to content

Commit

Permalink
feat(css): 🎉 initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
thierrymichel committed Feb 3, 2019
1 parent ec86612 commit aed8206
Show file tree
Hide file tree
Showing 14 changed files with 454 additions and 2 deletions.
1 change: 0 additions & 1 deletion packages/core/__tests__/transitions/manager.page.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ const nextHtml = `<html>
</html>`;

// Data

let data;

beforeEach(() => {
Expand Down
11 changes: 10 additions & 1 deletion packages/core/src/transitions/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,19 @@ export default {

this.running = true;

// CSS: add appear
// CSS: add appear-active
hooks.do('beforeAppear', data, t);
// CSS: remove appear
// CSS: add appear-to

t.beforeAppear && t.beforeAppear(data);
hooks.do('appear', data, t);

await runAsync(t.appear)(data)
.then(() => {
// CSS: remove appear-active
// CSS: remove appear-to
hooks.do('afterAppear', data, t);
t.afterAppear && t.afterAppear(data);
})
Expand Down Expand Up @@ -201,6 +208,8 @@ export default {
},

doLeave(t, data) {
// CSS: remove leave
// CSS: add leave-to
hooks.do('leave', data, t);

return runAsync(t.leave)(data).then(leaveResult => leaveResult);
Expand Down Expand Up @@ -242,8 +251,8 @@ export default {

doEnter(t, data, leaveResult) {
// CSS: remove enter
hooks.do('enter', data, t);
// CSS: add enter-to
hooks.do('enter', data, t);

return runAsync(t.enter)(data, leaveResult);
},
Expand Down
42 changes: 42 additions & 0 deletions packages/css/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"env": {
"commonjs": {
"presets": [
[
"@babel/env",
{
"targets": {
"browsers": ["last 2 versions", "ie >= 11"]
}
}
]
]
},
"esm": {
"presets": [
[
"@babel/env",
{
"modules": false
}
]
]
},
"umd": {
"presets": [
[
"@babel/env",
{
"modules": "commonjs",
"targets": {
"browsers": ["last 2 versions", "ie >= 11"]
}
}
]
]
},
"test": {
"presets": ["@babel/env"]
}
}
}
9 changes: 9 additions & 0 deletions packages/css/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### Custom ###
/__mocks__
/__tests__
/coverage
/src
/report.html
/*.md
!/README.md
!/CHANGELOG.md
2 changes: 2 additions & 0 deletions packages/css/AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Luigi De Rosa <lurukee@gmail.com> (http://luruke.com/)
Thierry Michel <thmichel@gmail.com> (https://www.epic.net/)
21 changes: 21 additions & 0 deletions packages/css/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Luigi De Rosa, Thierry Michel

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
19 changes: 19 additions & 0 deletions packages/css/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# @barba/css

[![NPM version](https://img.shields.io/npm/v/@barba/css.svg?style=flat-square)](https://www.npmjs.com/package/@barba/css)

> TBD ([GitHub repo](https://github.com/barbajs/barba-next.js))
## Install

Using npm:

```sh
npm install --save-dev @barba/css
```

or using yarn:

```sh
yarn add @barba/css --dev
```
107 changes: 107 additions & 0 deletions packages/css/__tests__/css.hooks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/* eslint-disable no-empty-function */
import barba from '@barba/core';
import css from '../src';

// Dom
const wrapper = document.createElement('div');
const container = document.createElement('div');

wrapper.dataset.barba = 'wrapper';
container.dataset.barba = 'container';

document.body.appendChild(wrapper);
document.body.appendChild(container);

// Transitions
const name = 'my-name';
const unnamed = {
appear() {},
leave() {},
enter() {},
};
const named = {
...unnamed,
name,
};

barba.use(css);
barba.init({
transitions: [named, unnamed],
});

/**
* Check CSS classes
*
* @param {string} [name='barba'] default or named transition
* @returns {Promise} end of hooks chain
*/
async function checkHooks(name = 'barba') {
expect(wrapper.classList.contains(`${name}-appear`)).toBeTruthy();
expect(wrapper.classList.contains(`${name}-appear-active`)).toBeTruthy();
await new Promise(resolve => {
window.requestAnimationFrame(() => {
expect(wrapper.classList.contains(`${name}-appear-to`)).toBeTruthy();
expect(wrapper.classList.contains(`${name}-appear`)).toBeFalsy();
resolve();
});
});
barba.hooks.do('afterAppear');
expect(wrapper.classList.contains(`${name}-appear-active`)).toBeFalsy();
expect(wrapper.classList.contains(`${name}-appear-to`)).toBeFalsy();
// Leave
barba.hooks.do('beforeLeave');
expect(wrapper.classList.contains(`${name}-leave`)).toBeTruthy();
expect(wrapper.classList.contains(`${name}-leave-active`)).toBeTruthy();
barba.hooks.do('leave');
await new Promise(resolve => {
window.requestAnimationFrame(() => {
expect(wrapper.classList.contains(`${name}-leave-to`)).toBeTruthy();
expect(wrapper.classList.contains(`${name}-leave`)).toBeFalsy();
resolve();
});
});
barba.hooks.do('afterLeave');
expect(wrapper.classList.contains(`${name}-leave-to`)).toBeFalsy();
expect(wrapper.classList.contains(`${name}-leave-active`)).toBeFalsy();
// Enter
barba.hooks.do('beforeEnter');
expect(wrapper.classList.contains(`${name}-enter`)).toBeTruthy();
expect(wrapper.classList.contains(`${name}-enter-active`)).toBeTruthy();
barba.hooks.do('enter');
await new Promise(resolve => {
window.requestAnimationFrame(() => {
expect(wrapper.classList.contains(`${name}-enter-to`)).toBeTruthy();
expect(wrapper.classList.contains(`${name}-enter`)).toBeFalsy();
resolve();
});
});
barba.hooks.do('afterEnter');
expect(wrapper.classList.contains(`${name}-enter-to`)).toBeFalsy();
expect(wrapper.classList.contains(`${name}-enter-active`)).toBeFalsy();
}

it('prefixes with transition name', () => {
barba.hooks.do('before', {}, named);
expect(css._prefix).toBe(name);
css._prefix = null;
barba.hooks.do('beforeAppear', {}, named);
expect(css._prefix).toBe(name);
});

it('prefixes with default ', () => {
barba.hooks.do('before', {}, unnamed);
expect(css._prefix).toBe('barba');
css._prefix = null;
barba.hooks.do('beforeAppear', {}, unnamed);
expect(css._prefix).toBe('barba');
});

it('adds and removes default CSS classes', async () => {
barba.hooks.do('beforeAppear', {}, unnamed);
await checkHooks();
});

it('adds and removes named CSS classes', async () => {
barba.hooks.do('beforeAppear', {}, named);
await checkHooks(name);
});
36 changes: 36 additions & 0 deletions packages/css/__tests__/css.init.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint-disable no-empty-function */
import barba from '@barba/core';
import css from '../src';
import { version } from '../package.json';

// Dom
const wrapper = document.createElement('div');
const container = document.createElement('div');

wrapper.dataset.barba = 'wrapper';
container.dataset.barba = 'container';

document.body.appendChild(wrapper);
document.body.appendChild(container);

it('has defaults', () => {
expect(css.version).toBe(version);
expect(css._prefix).toBe('barba');
expect(css._root).toBeUndefined();
});

it('has default root', () => {
barba._plugins = [];
barba.use(css);
barba.init();
expect(css._root).toBe(barba._wrapper);
});

it('has custom root', () => {
barba._plugins = [];
barba.use(css, {
root: document.body,
});
barba.init();
expect(css._root).toBe(document.body);
});
5 changes: 5 additions & 0 deletions packages/css/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const jestBase = require('../../jest.config.js');

module.exports = {
...jestBase,
};
42 changes: 42 additions & 0 deletions packages/css/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "@barba/css",
"version": "2.0.1-next.66+1463979",
"description": "Barba is a ...",
"publishConfig": {
"access": "public"
},
"browser": "dist/barba-css.min.js",
"main": "lib/index.js",
"module": "esm/index.js",
"files": [
"dist",
"lib",
"esm"
],
"keywords": [
"barba",
"css"
],
"homepage": "https://github.com/barbajs/barba-next#readme",
"bugs": {
"url": "https://github.com/barbajs/barba-next/issues"
},
"license": "MIT",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/barbajs/barba-next.git"
},
"scripts": {
"build": "npm-run-all build:commonjs build:umd build:umd:min build:esm",
"build:commonjs": "cross-env BABEL_ENV=commonjs babel src --out-dir lib",
"build:esm": "cpx 'src/**/*.{mjs,js,json}' esm",
"build:esm:watch": "cpx 'src/**/*.{mjs,js,json}' esm -w",
"build:umd": "cross-env BABEL_ENV=umd NODE_ENV=development webpack --progress --profile",
"build:umd:min": "cross-env BABEL_ENV=umd NODE_ENV=production webpack --progress --profile",
"clear": "rimraf dist esm lib",
"lint": "eslint src/**",
"precommit": "lint-staged",
"test": "eslint src/** && jest --coverage",
"watch": "jest --colors --watch"
}
}
Loading

0 comments on commit aed8206

Please sign in to comment.