Skip to content

Commit

Permalink
Add UseParent atom and Comment component; update README
Browse files Browse the repository at this point in the history
  • Loading branch information
tech_e committed Dec 5, 2024
1 parent 0070b49 commit 2e9f62d
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 4 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,20 @@ The callback function will be called when the data property changes. The callbac
}
```
### Use Parent Atom
The UseParent atom allows for the parent component to be accessed in a child atom. This atom is useful when a child atom needs to access the parent component.
```javascript
UseParent((parent) =>
{
// access the parent component
return Div({
class: parent.state.loaded ? 'loaded' : 'loading'
});
})
```
## Contributing
Contributions to Base Framework are welcome. Follow these steps to contribute:
Expand Down
3 changes: 2 additions & 1 deletion src/atoms.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Atom } from '@base-framework/base';
import { On, OnRoute, OnState } from './on/on.js';
export { On, OnRoute, OnState };
import { UseParent } from './use/use.js';
export { On, OnRoute, OnState, UseParent };

/**
* Creates a generic HTML tag.
Expand Down
11 changes: 11 additions & 0 deletions src/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* This will create a comment.
*
* @param {object} props
* @returns {object}
*/
export const Comment = (props) => ({
tag: 'comment',
textContent: `${props.type} placeholder`,
onCreated: props.onCreated
});
6 changes: 3 additions & 3 deletions src/on/on.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Builder, dataBinder } from "@base-framework/base";
import { Comment as BaseComment } from "src/comment.js";

/**
* This will set a previous result.
Expand Down Expand Up @@ -111,9 +112,8 @@ const updateLayout = (callBack, ele, prop, parent) =>
* @param {object} props
* @returns {object}
*/
const Comment = (props) => ({
tag: 'comment',
textContent: 'on placeholder',
const Comment = (props) => BaseComment({
type: 'on',
onCreated: props.onCreated
});

Expand Down
72 changes: 72 additions & 0 deletions src/use/use.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Builder } from "@base-framework/base";
import { Comment as BaseComment } from "src/comment.js";

/**
* This will set up the update layout function.
*
* @param {function} callBack
* @param {object} ele
* @param {object} parent
* @returns {function}
*/
const updateLayout = (callBack, ele, parent) =>
{
const layout = callBack(parent);
if (typeof layout === "undefined")
{
return;
}

/**
* This will build the layout and insert it after the
* comment element.
*/
const frag = Builder.build(layout, null, parent);
ele.parentNode.insertBefore(frag, ele.nextSibling);
};

/**
* This will create a comment.
*
* @param {object} props
* @returns {object}
*/
const Comment = (props) => BaseComment({
type: 'use',
onCreated: props.onCreated
});

/**
* This will create a use parent tag.
*
* @overload
* @param {object} data
* @param {string} prop
* @param {function} callBack
*
* @overload
* @param {string} prop
* @param {function} callBack
*
* @returns {object}
*/
export const UseParent = (...args) =>
{
const settings = [...args];
const callBack = settings.pop();
if (typeof callBack !== 'function')
{
return;
}

/**
* This will create a comment to use as a placeholder
* to keep the layout in place.
*/
return Comment({
onCreated: (ele, parent) =>
{
updateLayout(callBack, ele, parent);
}
});
};

0 comments on commit 2e9f62d

Please sign in to comment.