Complex primitives anatomy #185
Replies: 1 comment 5 replies
-
Yes, I've seen and analized on I see that sometimes the component approach is better than the directive one and in another time it is the other way around. It depends :) We could also expose the root of each funability as both, the component and the directive - like PrimeNG does. In our @Component({
standalone: true,
selector: '[rdxSelect]',
template: `
<ng-content select="[rdxSelectTrigger]" />
<ng-template
[cdkConnectedOverlayOpen]="open"
[cdkConnectedOverlayOrigin]="elementRef"
[cdkConnectedOverlayPositions]="positions"
[cdkConnectedOverlayScrollStrategy]="overlay.scrollStrategies.reposition()"
(attach)="onAttached()"
(backdropClick)="close()"
(detach)="onDetach()"
cdkConnectedOverlay
>
<ng-content select="[rdxSelectContent]" />
</ng-template>
`
// ...
}) So it works directive-like way but one downside is that that kind of selector cannot be applied to Concluding, I reckon for the component approach it would be better to use standard selector like this: @Component({
standalone: true,
selector: 'rdx-select',
template: `
<ng-content select="[rdxSelectTrigger]" />
<ng-template
[cdkConnectedOverlayOpen]="open"
[cdkConnectedOverlayOrigin]="elementRef"
[cdkConnectedOverlayPositions]="positions"
[cdkConnectedOverlayScrollStrategy]="overlay.scrollStrategies.reposition()"
(attach)="onAttached()"
(backdropClick)="close()"
(detach)="onDetach()"
cdkConnectedOverlay
>
<ng-content select="[rdxSelectContent]" />
</ng-template>
`
// ...
}) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
@mrfrac and I have been analyzing the structure of components, and here's what we've observed.
When we talk about something being "easy", we mean it's clear, requires minimal adjustments, and is straightforward for developers to use.
We’ve identified two main scenarios:
avatar
,separator
, etc., where we can easily use directives to implement the functionality.select
,slider
,radio
, etc., where there are multiple child elements and significant logic between them.For the second case, the approach used by @lskramarov in the Select component seems to work exceptionally well:
The
ng-content
mechanism is similar to slots in Web Components or children in React, but it offers more powerful customization capabilities through the select attribute.Benefits:
Explicit content separation.
Clearly defines and separates different parts of the component's content.
Flexibility and reusability
Developers can provide custom elements for triggers ([rdxSelectTrigger]) and content ([rdxSelectContent]).
Lifecycle management (enhanced with Signals API)
Event handlers like (attach) and (detach) on the ng-template enable efficient state management, such as initialization or resource cleanup.
This is particularly useful for components that interact with overlays, portals, or dynamic DOM elements.
Beta Was this translation helpful? Give feedback.
All reactions