-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
90 additions
and
46 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export default function ( element, templateName, transitions ) { | ||
if ( !transitions ) { | ||
throw new Error( "x-flux: No transitions found for " + templateName ); | ||
} | ||
|
||
for ( const directive of Object.keys( transitions ) ) { | ||
element.setAttribute( directive, transitions[directive] ); | ||
} | ||
|
||
element.removeAttribute( "x-flux" ); | ||
} |
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,18 @@ | ||
import parseArray from "../utils/parseArray"; | ||
import parseObject from "../utils/parseObject"; | ||
|
||
export default function (templateName, template = null) { | ||
if (!template) { | ||
throw new Error("x-flux: Template " + templateName + " does not exist in the config."); | ||
} | ||
|
||
try { | ||
if (Array.isArray(template)) { | ||
return parseArray(template); | ||
} | ||
|
||
return parseObject(template); | ||
} catch (error) { | ||
throw new Error("x-flux: Only accept array or object."); | ||
} | ||
} |
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,29 +1,28 @@ | ||
import parseArray from "./utils/parseArray"; | ||
import parseObject from "./utils/parseObject"; | ||
import applyTransitions from "./core/applyTransitions"; | ||
import parseTransitions from "./core/parseTransitions"; | ||
import convertCamelCase from "./utils/convertCamelCase"; | ||
|
||
export default function (Alpine, Config) { | ||
Alpine.directive("flux", (el, { expression }, { evaluate }) => { | ||
const arrayOrString = evaluate(expression); | ||
const transitions = | ||
(Array.isArray(arrayOrString) ? arrayOrString : Config[arrayOrString]) || | ||
null; | ||
export default function ( Alpine, Config ) { | ||
const magicNames = ['flux', ...Object.keys( Config )]; | ||
|
||
let attributes = {}; | ||
Alpine.directive( "flux", ( el, { expression }, { evaluate } ) => { | ||
const arrayOrTemplateName = evaluate( expression ); | ||
const template = | ||
( Array.isArray( arrayOrTemplateName ) ? arrayOrTemplateName : Config[arrayOrTemplateName] ) || | ||
null; | ||
const transitions = parseTransitions( arrayOrTemplateName, template ); | ||
|
||
if (!transitions) { | ||
throw new Error("x-flux: No transitions found for " + expression); | ||
} | ||
applyTransitions( el, arrayOrTemplateName, transitions ) | ||
} ).before( "transition" ); | ||
|
||
if (Object.prototype.toString.call(transitions) === "[object Object]") { | ||
attributes = parseObject(transitions); | ||
} else { | ||
attributes = parseArray(transitions); | ||
} | ||
for ( const templateName of magicNames ) { | ||
const validName = convertCamelCase( templateName ); | ||
|
||
Object.entries(attributes).forEach(([key, value]) => { | ||
el.setAttribute(key, value); | ||
}); | ||
Alpine.magic( validName, ( el ) => () => { | ||
const template = Config[templateName] || null; | ||
const transitions = parseTransitions( templateName, template ); | ||
|
||
el.removeAttribute("x-flux"); | ||
}).before("transition"); | ||
applyTransitions( el, templateName, transitions ) | ||
} ); | ||
} | ||
} |
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,13 @@ | ||
export default function (templateName) { | ||
const words = templateName.split('-'); | ||
|
||
if (words.length === 1) { | ||
return templateName; | ||
} | ||
|
||
const camelCasedWords = words.slice(1).map(word => { | ||
return word.charAt(0).toUpperCase() + word.slice(1); | ||
}); | ||
|
||
return words[0] + camelCasedWords.join(''); | ||
} |
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,17 +1,17 @@ | ||
export default function (transitions) { | ||
const [transition, enterStart, enterEnd, timingsIn = "", timingsOut = ""] = transitions; | ||
export default function ( transitions ) { | ||
const [transition, enterStart, enterEnd, timingsIn = "", timingsOut = ""] = transitions; | ||
|
||
const transitionEnter = `${transition} ${timingsIn}`.trim(); | ||
const transitionLeave = `${transition} ${timingsOut}`.trim(); | ||
const transitionEnter = `${transition} ${timingsIn}`.trim(); | ||
const transitionLeave = `${transition} ${timingsOut}`.trim(); | ||
|
||
const attrs = { | ||
"x-transition:enter": transitionEnter, | ||
"x-transition:enter-start": enterStart, | ||
"x-transition:enter-end": enterEnd, | ||
"x-transition:leave": transitionLeave, | ||
"x-transition:leave-start": enterEnd, | ||
"x-transition:leave-end": enterStart, | ||
}; | ||
const attrs = { | ||
"x-transition:enter": transitionEnter, | ||
"x-transition:enter-start": enterStart, | ||
"x-transition:enter-end": enterEnd, | ||
"x-transition:leave": transitionLeave, | ||
"x-transition:leave-start": enterEnd, | ||
"x-transition:leave-end": enterStart, | ||
}; | ||
|
||
return attrs; | ||
return attrs; | ||
} |
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,11 +1,11 @@ | ||
export default function (transitions) { | ||
let attributes = {}; | ||
export default function ( transitions ) { | ||
let attributes = {}; | ||
|
||
for (const key in transitions) { | ||
const classes = transitions[key]; | ||
for ( const key in transitions ) { | ||
const classes = transitions[key]; | ||
|
||
attributes[`x-transition:${key}`] = classes; | ||
} | ||
attributes[`x-transition:${key}`] = classes; | ||
} | ||
|
||
return attributes; | ||
return attributes; | ||
} |