0

I'm working in an Angular project. I have a component that has responsive functionality. We have logic setup so that we perform the needed responsive functionality whenever the window resizes. We use this to achieve that:

@HostListener("window:resize", ["$event"])
onResize(): void { ... }

That works great when the window sizes changes. However, now we've added a side panel to the page that can be collapsed. We want to perform the responsive functionality when the panel is expanded/collapsed. Or in other words, we want to perform our responsive logic when the size of the component changes, even if the window size is the same.

How can we achieve this?

6
  • 1
    I don't think there is any easy built-in method, as an element resize doesn't trigger a DOM event, unlike the window resize. But you could try out Resize Observer. Commented Sep 13, 2023 at 13:49
  • What about a sidePanelService, which hosts a subject, to which your component subscribe to know when it toggles ?
    – Random
    Commented Sep 13, 2023 at 14:32
  • @Random I'd like to have it more supportive than that. I don't want the responsive component to need knowledge of other elements on the page, it should simply be responsive within it's given space. Commented Sep 13, 2023 at 15:58
  • well, your component is already using @HostListener("window:resize", ["$event"]), so your component is checking for a resize event. The logic is already there. But what you could do also, is a "resizeService", which triggers an event when the window resize, and when the sidebar toggles. So your component would only subscribe to one event, and doesn't have to know who triggers it
    – Random
    Commented Sep 14, 2023 at 7:21
  • @Random the responsive component is a shared component, used in multiple other applications. I don't think requiring all apps to setup a 'resizeService' in order to use our component would be ideal. Looking for the component itself to keep track of this. Commented Sep 14, 2023 at 17:40

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.