In this post, I am going to dig a little deeper into working with the new Ionic Split Pane component. I explored it some in this post. There are three elements I want to explore: a full header design, understanding how to navigate pages, and handling resizing.
Full Header Design
Several readers asked about how you might achieve this design:
After playing around with different variations of the component structure and encountering some navigation troubles, I settled on using a basic CSS approach to this design.
The app.html is a standard component (sorry spacing with the brackets, blame WordPress):
This should look very close the documentation from Ionic. All we need to do is update the app.scss file with two changes:
We define a menuToggleState boolean to hold our pane’s visible state. This variable is set once the platform is ready. We have hard code the breakpoint to match the split panes. If you change that value, you will need to update it here as well.
The key to this solution is using the Ionic Events module to broadcast this value upon a state change triggered by the IonChange event. When that event occurs, we check the new width, and determine of the pane is now showing or not. Then we publish our custom event throughout our Ionic app.
In each page that is shown with the main content, I adjusted a few items. First, I had the title component is bound the title variable.
When we do not want a title visible, we set this variable to hold a space, rather than an empty string. The reason for this is, without content in the node, Ionic did not properly display any header icons. We also include a button that will auto display our hamburger menu icon when the split pane is hidden, giving us access to that content.
This is for the Project Mercury page, each NASA project page has the similar event subscriber included in its constructor. For this sample, I did not rework this into a custom component to properly encapsulate the code as to not need to repeat so much of it across all these pages. The basic structure of the code listens for the event from the master component and depending on the state updates the title variable.
Now when I resize the window smaller than the breakpoint, the title is updated on the main content pages. Here is what it looks like when resized:
and the menu being displayed:
Another tricky part of working with the split pane is understanding how to navigate pages. It was this issue that forced me to abandon several other attempts at a full header design. The heart of updating the main content from the pane is to use the @ViewChild.
Within the class for the app, we define our ViewChild to come from our root NavController using @ViewChild(Nav) nav: Nav;