Skip to content

Commit

Permalink
Merge pull request #287 from josemarluedke/feat/ref-util
Browse files Browse the repository at this point in the history
feat: add ref util
  • Loading branch information
josemarluedke authored Apr 5, 2024
2 parents 9325808 + 5e7cf88 commit 5df5c63
Show file tree
Hide file tree
Showing 10 changed files with 3,533 additions and 9,904 deletions.
4 changes: 2 additions & 2 deletions packages/utilities/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@
"ember-source": "^4.0.0 || ^5.0.0"
},
"dependencies": {
"@ember/render-modifiers": "^2.1.0",
"@ember/test-waiters": "^3.0.2",
"@embroider/addon-shim": "^1.8.7",
"@frontile/theme": "workspace:0.17.0-alpha.15",
"ember-auto-import": "^2.7.2",
"ember-element-helper": "^0.8.5",
"focus-visible": "^5.2.0"
"focus-visible": "^5.2.0",
"ember-modifier": "^4.1.0"
},
"devDependencies": {
"@babel/core": "7.24.0",
Expand Down
15 changes: 11 additions & 4 deletions packages/utilities/src/components/collapsible.gts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Component from '@glimmer/component';
import { action } from '@ember/object';
import { buildWaiter } from '@ember/test-waiters';
import { on } from '@ember/modifier';
import didUpdate from '@ember/render-modifiers/modifiers/did-update';
import { modifier } from 'ember-modifier';
import safeStyles from '../utils/safe-styles';

const waiter = buildWaiter('@frontile/utilities:collapsible');
Expand Down Expand Up @@ -66,7 +66,14 @@ class Collapsible extends Component<CollapsibleSignature> {
return safeStyles(styles);
}

@action update(element: HTMLElement, [isOpen]: boolean[]): void {
hasSetupUpdate = false;
update = modifier((element: HTMLElement, [isOpen]: boolean[]) => {
// do not run update on the initial setup of modifier
if (!this.hasSetupUpdate) {
this.hasSetupUpdate = true;
return;
}

if (this.isCurrentlyOpen !== !!isOpen) {
this.waiterToken = waiter.beginAsync();
}
Expand All @@ -76,7 +83,7 @@ class Collapsible extends Component<CollapsibleSignature> {
} else {
this.contract(element);
}
}
});

@action onTransitionEnd(event: TransitionEvent): void {
if (
Expand Down Expand Up @@ -157,7 +164,7 @@ class Collapsible extends Component<CollapsibleSignature> {
<div
style={{this.styles}}
...attributes
{{didUpdate this.update @isOpen}}
{{this.update @isOpen}}
{{on "transitionend" this.onTransitionEnd}}
>
{{yield}}
Expand Down
1 change: 1 addition & 0 deletions packages/utilities/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './components/visually-hidden';
export * from './components/collapsible';
export * from './components/divider';
export * from './components/spinner';
export * from './utils/ref';
23 changes: 23 additions & 0 deletions packages/utilities/src/utils/ref.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { tracked } from '@glimmer/tracking';
import { modifier } from 'ember-modifier';

class State<T> {
@tracked element: T | undefined;
}

function ref<T extends Element = Element>() {

Check warning on line 8 in packages/utilities/src/utils/ref.ts

View workflow job for this annotation

GitHub Actions / Tests

Missing return type on function
const state = new State<T>();
return {
get element(): T | undefined {
return state.element;
},
setup: modifier((el: T) => {
state.element = el;
return (): void => {
state.element = undefined;
};
})
};
}

export { ref };
5 changes: 1 addition & 4 deletions packages/utilities/unpublished-development-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@

import 'ember-source/types';
import '@glint/environment-ember-template-imports';
import type RenderModifiersRegistry from '@ember/render-modifiers/template-registry';
import type CoreRegistry from '../src/template-registry.ts';

declare module '@glint/environment-ember-loose/registry' {
export default interface Registry
extends RenderModifiersRegistry,
CoreRegistry {
export default interface Registry extends CoreRegistry {
// ...
}
}
Loading

0 comments on commit 5df5c63

Please sign in to comment.