@medyll/idae-be 0.87.0 → 0.89.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  # @medyll/idae-be
3
2
 
4
3
  A DOM walk and manipulation library with a callback-based approach for precise element targeting.
@@ -21,134 +20,155 @@ npm install @medyll/idae-be
21
20
 
22
21
  Unlike jQuery and other chained libraries, `@medyll/idae-be` always returns the root object. This approach allows for consistent chaining while using callbacks to manipulate targeted elements. This design provides more control and clarity in complex DOM operations.
23
22
 
23
+ ---
24
+
24
25
  ## Basic Usage
25
26
 
27
+ ### Example 1: DOM Manipulation with Callbacks
28
+
26
29
  ```javascript
27
30
  import { be, toBe } from '@medyll/idae-be';
28
31
 
32
+ // Select the container element
29
33
  be('#container')
30
- .append(toBe('<div>New content</div>'), ({ be }) => {
31
- be
32
- .addClass('highlight')
33
- .on('click', () => console.log('Clicked!'));
34
- })
35
- .prepend(toBe('<h1>Title</h1>'))
36
- .children(undefined, ({ be }) => {
37
- be.setStyle({ color: 'blue' });
38
- });
34
+ .append(toBe('<div>New content</div>'), ({ be }) => {
35
+ be.addClass('highlight')
36
+ .on('click', () => console.log('Clicked!'))
37
+ .append(toBe('<span>Nested content</span>'), ({ be }) => {
38
+ be.addClass('nested').on('mouseover', () => console.log('Hovered!'));
39
+ });
40
+ })
41
+ .prepend(toBe('<h1>Title</h1>'), ({ be }) => {
42
+ be.addClass('title').children(({ be }) => {
43
+ be.setStyle({ color: 'blue' });
44
+ });
45
+ });
39
46
  ```
40
47
 
41
- ## API Reference
42
-
43
- ### Core Methods
48
+ **Comments:**
44
49
 
45
- - `be(selector: string | HTMLElement | HTMLElement[]): Be` - Create a new Be instance
46
- - `toBe(str: string | HTMLElement, options?: { tag?: string }): Be` - Convert string or HTMLElement to Be instance
47
- - `createBe(tagOrHtml: string, options?: Object): Be` - Create a new Be element
50
+ 1. **`append`**: Adds a new `<div>` to the container and applies a class and event listener.
51
+ 2. **Nested `append`**: Adds a `<span>` inside the appended `<div>` with its own class and event listener.
52
+ 3. **`prepend`**: Adds a title at the beginning of the container and styles its children.
48
53
 
49
- ### DOM Manipulation
54
+ ---
50
55
 
51
- - `update(content: string): Be`
52
- - `append(content: string | HTMLElement): Be`
53
- - `prepend(content: string | HTMLElement): Be`
54
- - `insert(mode: 'afterbegin' | 'afterend' | 'beforebegin' | 'beforeend', element: HTMLElement | Be): Be`
55
- - `remove(): Be`
56
- - `replace(content: string | HTMLElement): Be`
57
- - `clear(): Be`
58
- - `normalize(): Be`
59
- - `wrap(tag?: string): Be`
56
+ ### Example 2: Event Handling and Traversal
60
57
 
61
- ### Traversal
62
-
63
- - `up(selector?: string, callback?: HandlerCallBackFn): Be`
64
- - `next(selector?: string, callback?: HandlerCallBackFn): Be`
65
- - `previous(selector?: string, callback?: HandlerCallBackFn): Be`
66
- - `siblings(selector?: string, callback?: HandlerCallBackFn): Be`
67
- - `children(selector?: string, callback?: HandlerCallBackFn): Be`
68
- - `closest(selector: string, callback?: HandlerCallBackFn): Be`
69
- - `firstChild(selector?: string, callback?: HandlerCallBackFn): Be`
70
- - `lastChild(selector?: string, callback?: HandlerCallBackFn): Be`
71
- - `find(selector: string, callback?: HandlerCallBackFn): Be`
72
- - `findAll(selector: string, callback?: HandlerCallBackFn): Be`
73
- - `without(selector: string, callback?: HandlerCallBackFn): Be`
58
+ ```javascript
59
+ import { be } from '@medyll/idae-be';
60
+
61
+ // Add a click event to all buttons inside the container
62
+ be('#container button').on('click', ({ target }) => {
63
+ be(target)
64
+ .toggleClass('active')
65
+ .siblings(({ be }) => {
66
+ be.removeClass('active').on('mouseover', () => console.log('Sibling hovered!'));
67
+ });
68
+ });
69
+
70
+ // Fire a custom event and handle it
71
+ be('#container').fire('customEvent', { detailKey: 'detailValue' }, ({ be }) => {
72
+ be.children(({ be }) => {
73
+ be.addClass('custom-event-handled');
74
+ });
75
+ });
76
+ ```
74
77
 
75
- ### Styling
78
+ **Comments:**
76
79
 
77
- - `setStyle(styles: Record<string, string>): Be`
78
- - `getStyle(property: string): string | null`
79
- - `unsetStyle(properties: string[]): Be`
80
- - `addClass(className: string): Be`
81
- - `removeClass(className: string): Be`
82
- - `toggleClass(className: string): Be`
83
- - `replaceClass(oldClass: string, newClass: string): Be`
80
+ 1. **`on`**: Adds a click event to buttons and toggles a class on the clicked button.
81
+ 2. **`siblings`**: Removes a class from sibling buttons and adds a hover event.
82
+ 3. **`fire`**: Dispatches a custom event and applies a class to all child elements.
84
83
 
85
- ### Events
84
+ ---
86
85
 
87
- - `on(eventName: string, handler: EventListener): Be`
88
- - `off(eventName: string, handler: EventListener): Be`
89
- - `fire(eventName: string, detail?: any): Be`
86
+ ### Example 3: Styling and Attributes
90
87
 
91
- ### Attributes and Properties
88
+ ```javascript
89
+ import { be } from '@medyll/idae-be';
90
+
91
+ // Select an element and update its styles and attributes
92
+ be('#element')
93
+ .setStyle({ backgroundColor: 'yellow', fontSize: '16px' }, ({ be }) => {
94
+ be.setAttr('data-role', 'admin').children(({ be }) => {
95
+ be.setStyle({ color: 'red' }).setAttr('data-child', 'true');
96
+ });
97
+ })
98
+ .addClass('styled-element', ({ be }) => {
99
+ be.siblings(({ be }) => {
100
+ be.setStyle({ opacity: '0.5' });
101
+ });
102
+ });
103
+ ```
92
104
 
93
- - `setAttr(name: string, value: string): Be`
94
- - `getAttr(name: string): string | null`
95
- - `deleteAttr(name: string): Be`
96
- - `setData(key: string, value: string): Be`
97
- - `getData(key: string): string | null`
98
- - `deleteData(key: string): Be`
105
+ **Comments:**
99
106
 
100
- ### Timers
107
+ 1. **`setStyle`**: Applies styles to the selected element and its children.
108
+ 2. **`setAttr`**: Sets attributes on the selected element and its children.
109
+ 3. **`addClass`**: Adds a class to the element and modifies its siblings.
101
110
 
102
- - `timeout(delay: number, callback: HandlerCallBackFn): Be`
103
- - `interval(delay: number, callback: HandlerCallBackFn): Be`
104
- - `clearTimeout(): Be`
105
- - `clearInterval(): Be`
111
+ ---
106
112
 
107
- ## Advanced Example
113
+ ### Example 4: Timers
108
114
 
109
115
  ```javascript
110
- be('#app')
111
- .append(toBe('<div id="content"></div>'), ({ be }) => {
112
- be
113
- .append(toBe('<button>Add Item</button>'), ({ be }) => {
114
- be.on('click', () => {
115
- content.append(toBe('<p>New item</p>'), ({ be }) => {
116
- be
117
- .addClass('item')
118
- .setStyle({ color: 'blue' })
119
- .on('click', ({ target }) => {
120
- be(target).toggleClass('selected');
121
- });
122
- });
123
- });
124
- })
125
- .children('.item', ({ be }) => {
126
- be.setStyle({ padding: '5px', margin: '2px' });
127
- });
128
- })
129
- .up(({ be }) => {
130
- be.setStyle({ backgroundColor: '#f0f0f0' });
131
- });
116
+ import { be } from '@medyll/idae-be';
117
+
118
+ // Set a timeout to execute a callback after 100ms
119
+ be('#test').timeout(100, ({ be }) => {
120
+ be.setStyle({ backgroundColor: 'yellow' }).append('<span>Timeout executed</span>');
121
+ });
122
+
123
+ // Set an interval to execute a callback every 100ms
124
+ be('#test').interval(100, ({ be }) => {
125
+ be.toggleClass('highlight');
126
+ });
127
+
128
+ // Clear a timeout before it executes
129
+ const timeoutInstance = be('#test').timeout(500, ({ be }) => {
130
+ be.setStyle({ color: 'red' });
131
+ });
132
+ timeoutInstance.clearTimeout();
133
+
134
+ // Clear an interval after 600ms
135
+ const intervalInstance = be('#test').interval(400, ({ be }) => {
136
+ be.setStyle({ fontSize: '20px' });
137
+ });
138
+ intervalInstance.clearInterval();
132
139
  ```
133
140
 
134
- This example demonstrates:
135
- 1. Nested element creation and manipulation
136
- 2. Event handling with dynamic content addition
137
- 3. Traversal and bulk operations on children
138
- 4. Accessing and styling parent elements
139
-
140
- ## TypeScript Support
141
-
142
- This library is written in TypeScript and includes type definitions for a great developer experience.
141
+ ---
143
142
 
144
- ## Browser Support
143
+ ### Example 5: Walk
145
144
 
146
- Designed for modern browsers. May require polyfills for older browser support.
147
-
148
- ## Contributing
145
+ ```javascript
146
+ import { be } from '@medyll/idae-be';
147
+
148
+ // Traverse up the DOM tree to find the parent element
149
+ be('#child').up('#parent', ({ be: parent }) => {
150
+ parent.addClass('highlight').children(({ be: child }) => {
151
+ child.setStyle({ color: 'blue' });
152
+ });
153
+ });
154
+
155
+ // Find all siblings of an element and add a class
156
+ be('#target').siblings(({ be: siblings }) => {
157
+ siblings.addClass('sibling-class').children(({ be }) => {
158
+ be.setStyle({ fontWeight: 'bold' });
159
+ });
160
+ });
161
+
162
+ // Find the closest ancestor matching a selector
163
+ be('#child').closest('.ancestor', ({ be: closest }) => {
164
+ closest.setStyle({ border: '2px solid red' }).children(({ be }) => {
165
+ be.addClass('ancestor-child');
166
+ });
167
+ });
168
+ ```
149
169
 
150
- Contributions are welcome! Please submit pull requests or open issues on our GitHub repository.
170
+ ---
151
171
 
152
172
  ## License
153
173
 
154
- This project is licensed under the MIT License.
174
+ This project is licensed under the MIT License.
@@ -21,6 +21,7 @@ export declare class AttrHandler implements CommonHandler<AttrHandler, AttrHandl
21
21
  * @param element - The Be element to operate on.
22
22
  */
23
23
  constructor(element: Be);
24
+ methods: string[] | keyof AttrHandler;
24
25
  /**
25
26
  * Handles dynamic method calls for attribute operations.
26
27
  * @param actions - The actions to perform (e.g., set, get, delete).
@@ -18,6 +18,7 @@ export class AttrHandler {
18
18
  constructor(element) {
19
19
  this.beElement = element;
20
20
  }
21
+ methods = AttrHandler.methods;
21
22
  /**
22
23
  * Handles dynamic method calls for attribute operations.
23
24
  * @param actions - The actions to perform (e.g., set, get, delete).
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@medyll/idae-be",
3
3
  "scope": "@medyll",
4
- "version": "0.87.0",
4
+ "version": "0.89.0",
5
5
  "description": "A powerful DOM manipulation library with a callback-based approach for precise element targeting. Provides consistent chaining, comprehensive DOM traversal, event handling, style management, and more. Written in TypeScript for modern browsers.",
6
6
  "scripts": {
7
7
  "dev": "vite dev",