Skip to content

Commit

Permalink
Prefer arrow functions everywhere and use standard classes in build
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwep committed Nov 19, 2024
1 parent 16b7e93 commit 15c089d
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 27 deletions.
5 changes: 5 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ts from 'typescript-eslint';
import react from 'eslint-plugin-react';
import vue from 'eslint-plugin-vue';
import tsParser from '@typescript-eslint/parser';
import preferArrowFunctions from 'eslint-plugin-prefer-arrow-functions';

export default [
js.configs.recommended,
Expand All @@ -21,13 +22,17 @@ export default [
}
}
},
plugins: {
'prefer-arrow-functions': preferArrowFunctions
},
settings: {
react: {
version: 'detect'
}
},
rules: {
'@typescript-eslint/no-non-null-assertion': 'off',
'prefer-arrow-functions/prefer-arrow-functions': 'error',
'react/react-in-jsx-scope': 'off',
'react/prop-types': 'off',
'no-console': 'error',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@vue/compiler-sfc": "3.5.12",
"@vue/eslint-config-typescript": "14.1.3",
"eslint": "9.13.0",
"eslint-plugin-prefer-arrow-functions": "3.4.1",
"eslint-plugin-react": "7.37.2",
"eslint-plugin-vue": "9.30.0",
"lerna": "8.1.8",
Expand Down
6 changes: 3 additions & 3 deletions packages/preact/demo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import {useState} from 'preact/hooks';
import SelectionArea, {SelectionEvent} from '../src';
import './index.css';

function SelectableArea({boxes, offset, className}: {
const SelectableArea = ({boxes, offset, className}: {
boxes: number;
offset: number;
className: string;
}) {
}) => {
const [selected, setSelected] = useState<Set<number>>(() => new Set());
const extractIds = (els: Element[]): number[] =>
els.map(v => v.getAttribute('data-key'))
Expand Down Expand Up @@ -42,7 +42,7 @@ function SelectableArea({boxes, offset, className}: {
))}
</SelectionArea>
);
}
};

render(
<>
Expand Down
6 changes: 3 additions & 3 deletions packages/react/demo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import {createRoot} from 'react-dom/client';
import SelectionArea, {SelectionEvent} from '../src';
import './index.css';

function SelectableArea({boxes, offset, className}: {
const SelectableArea = ({boxes, offset, className}: {
boxes: number;
offset: number;
className: string;
}) {
}) => {
const [selected, setSelected] = useState<Set<number>>(() => new Set());

const extractIds = (els: Element[]): number[] =>
Expand Down Expand Up @@ -43,7 +43,7 @@ function SelectableArea({boxes, offset, className}: {
))}
</SelectionArea>
);
}
};


const root = createRoot(document.getElementById('root') as HTMLElement);
Expand Down
2 changes: 1 addition & 1 deletion packages/vanilla/src/EventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class EventTarget<Events extends EventMap> {
return this;
}

public dispatchEvent<K extends keyof Events>(event: K, ...data: Parameters<Events[K]>): unknown {
public dispatchEvent<K extends keyof Events>(event: K, ...data: Parameters<Events[K]>): boolean {
let ok = true;
for (const cb of (this._listeners.get(event) ?? [])) {
ok = (cb(...data) !== false) && ok;
Expand Down
12 changes: 2 additions & 10 deletions packages/vanilla/src/utils/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,17 @@ const unitify = (val: string | number, unit = 'px'): string => {
* @param val The value for a single attribute.
* @returns {*}
*/
export function css(
{style}: HTMLElement,
attr: Partial<Record<keyof CSSStyleDeclaration, string | number>> | string,
val?: string | number
): void {
export const css = ({style}: HTMLElement, attr: Partial<Record<keyof CSSStyleDeclaration, string | number>> | string, val?: string | number): void => {
if (typeof attr === 'object') {

for (const [key, value] of Object.entries(attr)) {
if (value !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
style[key as any] = unitify(value);
}

}

} else if (val !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
style[attr as any] = unitify(val);
}
}

};

1 change: 0 additions & 1 deletion packages/vanilla/src/utils/domRect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
export const domRect = (x = 0, y = 0, width = 0, height = 0): DOMRect => {
const rect = {x, y, width, height, top: y, left: x, right: x + width, bottom: y + height};
const toJSON = () => JSON.stringify(rect);

return {...rect, toJSON};
};
4 changes: 2 additions & 2 deletions packages/vanilla/src/utils/frames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const frames = <F extends AnyFunction>(fn: F): Frames<F> => {
let lock = false;

return {
next(...args: Parameters<F>): void {
next: (...args: Parameters<F>): void => {
previousArgs = args;

if (!lock) {
Expand All @@ -24,7 +24,7 @@ export const frames = <F extends AnyFunction>(fn: F): Frames<F> => {
});
}
},
cancel() {
cancel: () => {
cancelAnimationFrame(frameId);
lock = false;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/vanilla/src/utils/intersects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type Intersection = 'center' | 'cover' | 'touch'
* @param mode Options are center, cover or touch.
* @returns {boolean} If both elements intersects each other.
*/
export function intersects(a: DOMRect, b: DOMRect, mode: Intersection = 'touch'): boolean {
export const intersects = (a: DOMRect, b: DOMRect, mode: Intersection = 'touch'): boolean => {
switch (mode) {
case 'center': {
const bxc = b.left + b.width / 2;
Expand All @@ -31,4 +31,4 @@ export function intersects(a: DOMRect, b: DOMRect, mode: Intersection = 'touch')
a.top <= b.bottom;
}
}
}
};
4 changes: 2 additions & 2 deletions packages/vanilla/src/utils/selectAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type SelectAllSelectors = readonly (string | Element)[] | string | Elemen
* @param doc
* @returns {Array} Array of DOM-Nodes.
*/
export function selectAll(selector: SelectAllSelectors, doc: Document = document): Element[] {
export const selectAll = (selector: SelectAllSelectors, doc: Document = document): Element[] => {
const list = !Array.isArray(selector) ? [selector] : selector;
let nodes: Element[] = [];

Expand All @@ -25,4 +25,4 @@ export function selectAll(selector: SelectAllSelectors, doc: Document = document
}

return nodes;
}
};
4 changes: 2 additions & 2 deletions packages/vanilla/src/utils/shouldTrigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {Trigger} from '../types';
* @param triggers A list of Triggers that signify that the event should execute until completion
* @returns Whether the MouseEvent should execute until completion
*/
export function shouldTrigger(event: MouseEvent, triggers: Trigger[]): boolean {
export const shouldTrigger = (event: MouseEvent, triggers: Trigger[]): boolean => {
for (const trigger of triggers) {
// The trigger requires only a specific button to be pressed
if (typeof trigger === 'number') {
Expand Down Expand Up @@ -36,4 +36,4 @@ export function shouldTrigger(event: MouseEvent, triggers: Trigger[]): boolean {

// By default, we do not process the event
return false;
}
};
2 changes: 1 addition & 1 deletion packages/vanilla/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"include": ["src", "demo"],
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"useDefineForClassFields": false,
"module": "ESNext",
"lib": ["ESNext", "DOM"],
"moduleResolution": "Node",
Expand Down
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 15c089d

Please sign in to comment.