Hi all, I’m setting up a project using the latest version of Angular, I have installed the Design System using npm and all the html and css work great but it can’t use the javascript library as imported from a script tag,
Wondered if anyone had experience including the JS library in an Angular project or if there’s a TypeScript version. Read a few StackOverflow answers but haven’t managed to get any of their import methods working yet.
Hi @Gburnett we managed to convert the NSW DS components to React. Although it’s not the best approach, we’ve done this by initialising the component JS inside the lifecycle hook componentDidMount, in your case it will be on ngOnInit.
Have a look the React components here for tabs and main navigation components
And here’s how you can initialise each of the components.
I attempted to tap into the document.onreadystatechange and add events using document.addEventListener, but neither would execute consistently, particularly on route changes.
I have also attempted different combinations of calling window.NSW.initSite(), or using the aforementioned dom events, from within the AppComponent’s lifecycle hooks ngOnInit(), ngAfterViewInit(), and ngAfterViewChecked() but for various reasons, the initSite() method was either called too early, mid way through rendering/dom manipulation, too late, multiple times, or not at all.
This solution is working for me on any navigation/refresh and any route changes.
I will be investigating more elegant solutions, such as moving this logic to a router interceptor (like canActivate), but this works for now.
Note: I have not yet attempted to add any nsw design elements to the page dynamically (e.g. clicking an a button that adds an accordian element) that require the javascript init.
Perfect @angus this worked for me and I do have an accordion and mega menu implemented and can confirm they’re working too. Very happy to use this for now and great to go into the Easter weekend with this problem solved
Thanks for the suggestion too @benjamin , much appreciated!