blazorsidebarsyncfusion

Replace the default sidebar in .Net8 Blazor Web App with the Syncfusion sidebar


I have created Blazor Web App using Syncfusion extension template (24.2.3) with these configurations
Version: .Net8
Theme: Bootstrap v5
Authentication: Individual Accounts
Interactivity type: Server
Interactivity location: Per page/component
I have old project version (21.2.6) with Syncfusion sidebar and it is working perfectly so i want to get the same functionality in my new project
So, I replace this code from MainLayout.razor

    <div class="page">
    <div class="sidebar">
        <NavMenu />
    </div> 
    <main>
        <div class="top-row px-4">
        <CultureSwitcher/> 
        </div>

        <article class="content px-4">
            @Body
        </article>
    </main>
    </div>
    <div id="blazor-error-ui">
    An unhandled error has occurred.
    <a href="" class="reload">Reload</a>
    <a class="dismiss">🗙</a>
</div>

With this code from my old project

<SfSidebar ID="Sidebar" Type="SidebarType.Push" Width="300px" DockSize="65px" EnableDock="true" @ref="Sidebar" @bind-IsOpen="SidebarToggle">
    <div class="sidebar" style="position:fixed;z-index:1200">
        <div class="topnav navbar navbar-dark">
            <button class="navbar-toggler" @onclick="ToggleNavMenu" />
        </div>
        <div @onmouseover="MouseIn" @onmouseout="MouseOut">
            <NavMenu />
        </div>
    </div>
</SfSidebar>

<div class="main">
    <div class="top-row px-4" style="z-index:1111;padding-top:0px">
        
    </div>

    <div class="content px-4">
        @Body
    </div>
</div>
@code
{
    SfSidebar Sidebar;
    public bool SidebarToggle = false;
    public bool SidebarToggleFixed = false;
    public void ToggleNavMenu()
    {
        SidebarToggle = !SidebarToggle;
        SidebarToggleFixed = !SidebarToggleFixed;
    }
    public async Task MouseIn(Microsoft.AspNetCore.Components.Web.MouseEventArgs args)
    {
        var isSidebar = await JS.InvokeAsync<bool>("isSidebar", args.ClientX, args.ClientY);
        if (!SidebarToggle && isSidebar)
        {
            SidebarToggle = true;
        }
    }
    public async Task MouseOut(Microsoft.AspNetCore.Components.Web.MouseEventArgs args)
    {
        var isSidebar = await JS.InvokeAsync<bool>("isSidebar", args.ClientX, args.ClientY);
        if (SidebarToggle && !isSidebar)
        {
            if (!SidebarToggleFixed)
            {
                SidebarToggle = false;
            }
        }
    }
}

this is the css code

.sidebar {
    width: 300px;
    min-width: 300px;
}

.navbar-toggler {
    display: inline-block;
    border: none;
    width: 24px;
    height: 24px;
    margin-left: 30px;
    background-image: url('images/ToggleButton.png');
    background-color: #05163C;
    transform: scale(0.75);
    transform-origin: left;
    color: #ffffff;
}


/* Content area styles */
.title {
    font-size: 20px;
}

/* Sidebar styles */
.e-sidebar .e-icons::before {
    font-size: 25px;
}

.e-sidebar:hover .e-open {
    display: inline-block;
}

.e-sidebar:hover .e-close {
    display: none;
}
/* dockbar icon Style */
.e-sidebar .home::before {
    content: '\e102';
}

.e-sidebar .profile::before {
    content: '\e10c';
}

.e-sidebar .info::before {
    content: '\e11b';
}

.e-sidebar .settings::before {
    content: '\e10b';
}

.e-sidebar .expand::before,
.e-sidebar.e-right.e-open .expand::before {
    content: '\e10f';
}

.e-sidebar.e-open .expand::before,
.e-sidebar.e-right .expand::before {
    content: '\e10e';
}

/* end of dockbar icon Style */

.e-sidebar.e-dock.e-close span.e-text {
    display: none;
}

.e-sidebar.e-dock.e-close .navbar-toggler {
    margin-left: 21px;
}

.e-sidebar.e-dock.e-close .nav.flex-column {
    margin-left: -8px;
}

.e-sidebar.e-dock.e-close .displayText {
    display: none;
}

.e-sidebar.e-dock.e-close .nav-item.px-3 {
    width: 80px;
}

.e-sidebar.e-dock.e-close .separator {
    border: 1px solid #FFFFFF;
    opacity: 0.14;
    width: 45px;
    margin: 1rem;
    margin-top: 0px;
    margin-left: 0.5rem;
}

.e-sidebar.e-dock.e-close .oi.oi-home {
    margin-left: -1px;
}

.e-sidebar.e-dock.e-close .syncfusion-blazor-icons {
    margin-left: -1px;
}

.e-sidebar.e-dock.e-open span.e-text {
    display: inline-block;
}

.e-sidebar li {
    list-style-type: none;
    cursor: pointer;
}

.e-sidebar ul {
    padding: 0px;
}


.e-sidebar span.e-icons {
    color: #c0c2c5;
    line-height: 2
}

.e-open .e-icons {
    margin-right: 16px;
}

.e-open .e-text {
    overflow: hidden;
    font-size: 15px;
}

.sidebar-item {
    text-align: center;
    border-bottom: 1px #e5e5e58a solid;
}

.e-sidebar.e-open .sidebar-item {
    text-align: left;
    padding-left: 15px;
    color: #c0c2c5;
}

.e-sidebar {
    background: #2d323e;
    overflow: hidden;
}

@@font-face {
    font-family: 'e-icons';
    src: url(data:application/x-font-ttf;charset=utf-8;base64,AAEAAAAKAIAAAwAgT1MvMjciQ6oAAAEoAAAAVmNtYXBH1Ec8AAABsAAAAHJnbHlmKcXfOQAAAkAAAAg4aGVhZBLt+DYAAADQAAAANmhoZWEHogNsAAAArAAAACRobXR4LvgAAAAAAYAAAAAwbG9jYQukCgIAAAIkAAAAGm1heHABGQEOAAABCAAAACBuYW1lR4040wAACngAAAJtcG9zdEFgIbwAAAzoAAAArAABAAADUv9qAFoEAAAA//UD8wABAAAAAAAAAAAAAAAAAAAADAABAAAAAQAAlbrm7l8PPPUACwPoAAAAANfuWa8AAAAA1+5ZrwAAAAAD8wPzAAAACAACAAAAAAAAAAEAAAAMAQIAAwAAAAAAAgAAAAoACgAAAP8AAAAAAAAAAQPqAZAABQAAAnoCvAAAAIwCegK8AAAB4AAxAQIAAAIABQMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAUGZFZABA4QLhkANS/2oAWgPzAJYAAAABAAAAAAAABAAAAAPoAAAD6AAAA+gAAAPoAAAD6AAAA+gAAAPoAAAD6AAAA+gAAAPoAAAD6AAAAAAAAgAAAAMAAAAUAAMAAQAAABQABABeAAAADgAIAAIABuEC4QnhD+ES4RvhkP//AADhAuEJ4QvhEuEa4ZD//wAAAAAAAAAAAAAAAAABAA4ADgAOABYAFgAYAAAAAQACAAYABAADAAgABwAKAAkABQALAAAAAAAAAB4AQABaAQYB5gJkAnoCjgKwA8oEHAAAAAIAAAAAA+oDlQAEAAoAAAEFESERCQEVCQE1AgcBZv0mAXQB5P4c/g4Cw/D+lwFpAcP+s24BTf6qbgAAAAEAAAAAA+oD6gALA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G4AcwAtAG0AZQB0AHIAbwBlAC0AaQBjAG8AbgBzAC0AbQBlAHQAcgBvAFYAZQByAHMAaQBvAG4AIAAxAC4AMABlAC0AaQBjAG8AbgBzAC0AbQBlAHQAcgBvAEYAbwBuAHQAIABnAGUAbgBlAHIAYQB0AGUAZAAgAHUAcwBpAG4AZwAgAFMAeQBuAGMAZgB1AHMAaQBvAG4AIABNAGUAdAByAG8AIABTAHQAdQBkAGkAbwB3AHcAdwAuAHMAeQBuAGMAZgB1AHMAaQBvAG4ALgBjAG8AbQAAAAACAAAAAAAAAAoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAwBAgEDAQQBBQEGAQcBCAEJAQoBCwEMAQ0AB2hvbWUtMDELQ2xvc2UtaWNvbnMHbWVudS0wMQR1c2VyB0JUX2luZm8PU2V0dGluZ19BbmRyb2lkDWNoZXZyb24tcmlnaHQMY2hldnJvbi1sZWZ0CE1UX0NsZWFyDE1UX0p1bmttYWlscwRzdG9wAAA=) format('truetype');
    font-weight: normal;
    font-style: normal;
}

And I added this script to App.razor page

<body>
    <Routes />
    <script>
        function isSidebar(x, y) {
            var sidebarEle = document.elementFromPoint(x, y);
            return (sidebarEle && sidebarEle.closest('.sidebar') != null) ? true : false;
        }
    </script>
    <script src="_framework/blazor.web.js"></script>
</body>

But after run the application the sidebar does not appear at all


Solution

  • Based on the shared details, we understand that your requirement is to render the Blazor Sidebar component on the .NET 8 Web Authentication application MainLayout.razor page. To achieve this, directly render the Blazor Sidebar component on the MainLayout.razor page in the .NET 8 application, and you just need to add the @rendermode InteractiveServer in the .NET 8 Web Authentication application Routes.razor page at your end.

    [Routes.razor]

    @rendermode InteractiveServer ...

    For your reference, we have attached the prepared sample based on the code snippets you shared.

    Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/BlazorWebAppAuthentication1844180527.zip