Skip to content

Commit

Permalink
feat: define template inside $flux magic
Browse files Browse the repository at this point in the history
  • Loading branch information
iniznet committed Feb 17, 2023
1 parent ce7c780 commit 117ea12
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 25 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,17 @@ Alpine.plugin(() => {
Alpine.start()
```

## Example
## Usage

There's three ways to use this plugin.
There's several ways to use this plugin.

```html
<section x-data="{ show: false }">
<section x-data="{ show: false, template: [
'transition duration-300',
'opacity-0 scale-90',
'opacity-100 scale-100',
'ease-out', 'ease-in',
] }">
<button @click="show = !show">Toggle</button>

<!-- You can call the defined template by calling the name of the config -->
Expand All @@ -63,14 +68,21 @@ There's three ways to use this plugin.
<!-- Or you can use the magic -->
<div x-show="show" x-init="$translateY2">Array from Config</div>
<div x-show="show" x-init="$rotate">Object from Config</div>
<div x-show="show" x-init="$flux('translate-y-2')">Array from Config</div>

<!-- Or you can define it inline -->
<!-- You can define it inline -->
<div x-show="show" x-flux="[
'transition duration-300',
'opacity-0 scale-90',
'opacity-100 scale-100',
'ease-out', 'ease-in',
]">Array Expression</div>

<!-- You can define template with $flux magic, the name should be kebab-case when defining -->
<div x-show="show" x-init="$flux('opacity-scale', template)">Array Expression</div>

<!-- by default after defining it will applied to the element automatically, you can pass false as third argument to only register -->
<div x-show="show" x-init="$flux('opacity-scale', template, false)">Array Expression</div>
</section>
```

Expand Down
2 changes: 1 addition & 1 deletion dist/flux.esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/flux.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "alpinejs-flux",
"version": "1.1.0",
"description": "Simplifies x-transition when applying CSS classes",
"version": "1.2.0",
"description": "Simplifies the process of applying CSS classes with x-transition as a template and makes them reusable within single attribute.",
"keywords": [
"Alpine",
"Alpine JS",
Expand Down
6 changes: 5 additions & 1 deletion src/core/applyTransitions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export default function ( element, templateName, transitions ) {
import parseTransitions from "./parseTransitions";

export default function ( element, templateName, template ) {
const transitions = parseTransitions( templateName, template );

if ( !transitions ) {
throw new Error( "x-flux: No transitions found for " + templateName );
}
Expand Down
7 changes: 7 additions & 0 deletions src/core/registerMagic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import applyTransitions from "./applyTransitions";

export default function (Alpine, validName, templateName, template = null) {
Alpine.magic( validName, ( el ) => () => {
applyTransitions( el, templateName, template );
} );
}
43 changes: 27 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
import applyTransitions from "./core/applyTransitions";
import parseTransitions from "./core/parseTransitions";
import registerMagic from "./core/registerMagic";
import convertCamelCase from "./utils/convertCamelCase";

export default function ( Alpine, Config ) {

Alpine.directive( "flux", ( el, { expression }, { evaluate } ) => {
const arrayOrTemplateName = evaluate( expression );
const template =
( Array.isArray( arrayOrTemplateName ) ? arrayOrTemplateName : Config[arrayOrTemplateName] ) ||
null;
const transitions = parseTransitions( arrayOrTemplateName, template );
( Array.isArray( arrayOrTemplateName )
? arrayOrTemplateName
: Config[arrayOrTemplateName] ) || null;

applyTransitions( el, arrayOrTemplateName, transitions )
applyTransitions( el, arrayOrTemplateName, template );
} ).before( "transition" );

for ( const templateName of Object.keys( Config ) ) {
const validName = convertCamelCase( templateName );
const template = Config[templateName] || null;

Alpine.magic( validName, ( el ) => () => {
const template = Config[templateName] || null;
const transitions = parseTransitions( templateName, template );

applyTransitions( el, templateName, transitions )
} );
registerMagic( Alpine, validName, templateName, template );
}

Alpine.magic( 'flux', ( el ) => ( templateName = '' ) => {
const template = Config[templateName] || null;
const transitions = parseTransitions( templateName, template );
Alpine.magic(
"flux",
( el ) =>
( templateName = "", newTemplate = null, applyToElement = true ) => {
if ( newTemplate ) {
const validName = convertCamelCase( templateName );

Config[templateName] = newTemplate;
registerMagic( Alpine, validName, templateName, newTemplate );
}

applyTransitions( el, templateName, transitions )
} );
if ( !applyToElement ) {
return;
}

const template = Config[templateName] || null;

applyTransitions( el, templateName, template );
}
);
}

0 comments on commit 117ea12

Please sign in to comment.