themesberg / flowbite-react

Official React components built for Flowbite and Tailwind CSS
https://flowbite-react.com
MIT License
1.9k stars 426 forks source link

Difficulty Overriding Default Styles in Flowbite React for Navbar Component #1455

Open avishkakavindu opened 4 months ago

avishkakavindu commented 4 months ago

Steps to reproduce

  1. Create a react typescript project.
  2. Configure scss, tailwind and flowbite react
  3. Create a Navbar component
import React from 'react';
import { Link, useLocation } from 'react-router-dom';
import { Navbar } from 'flowbite-react';

import logo from '@assets/images/logo_blue_bg_none.png';
import { ROUTE_PATHS } from '@routes/paths';
import { PrimaryButton } from '@components/common/buttons';

function PublicNavBar() {
  const location = useLocation();

  return (
    <div className="public-navbar-container">
      <Navbar fluid rounded className="public-navbar">
        <div className="mx-auto flex w-full max-w-screen-xl flex-wrap items-center justify-between">
          <Navbar.Brand as={Link} href={ROUTE_PATHS.PUBLIC.HOME}>
            <img src={logo} className="mr-3 h-6 sm:h-9" alt="Logo" />
          </Navbar.Brand>

          <div className="flex-grow"></div>

          <div className="flex md:order-2">
            <PrimaryButton>Log In</PrimaryButton>
            <Navbar.Toggle />
          </div>

          <Navbar.Collapse>
            <Navbar.Link
              href="#"
              className={`public-navbar__link ${location.pathname === ROUTE_PATHS.PUBLIC.SIGN_IN ? 'public-navbar__link--active' : ''}`}
            >
              Home
            </Navbar.Link>
            <Navbar.Link
              as={Link}
              href="#"
              className={`public-navbar__link ${location.pathname === ROUTE_PATHS.PUBLIC.ABOUT ? 'public-navbar__link--active' : ''}`}
            >
              About
            </Navbar.Link>
            <Navbar.Link
              href="#"
              className={`public-navbar__link ${location.pathname === ROUTE_PATHS.PUBLIC.MEMBERSHIP ? 'public-navbar__link--active' : ''}`}
            >
              Membership
            </Navbar.Link>
            <Navbar.Link href={ROUTE_PATHS.PUBLIC.SIGN_IN}></Navbar.Link>
          </Navbar.Collapse>
        </div>
      </Navbar>
    </div>
  );
}

export default PublicNavBar;

navBar.scss

public-navbar-container {
  .public-navbar {
    &__link {
      color: red;
      &--active {
        @apply font-semibold text-primary-500;
      }

      &:hover {
        @apply font-semibold text-primary-500;
      }
    }
  }
}
  1. Go to the page and check for custom styles.
  2. Add !important and recheck

Current behavior

In the flowbite-react library, the default styles for the Navbar component are proving difficult to override. Specifically, the styles for the Navbar.Link elements cannot be customized as expected.

Expected behavior

SutuSebastian commented 4 months ago

The best way to style any flowbite-react component is by passing className (which usually directs the prop to the root element of the component) and if the component has multiple internals/slots, use the theme prop.

In the docs each element has a theme section which u can follow and see what tailwind classes have been applied in order to style it.

avishkakavindu commented 4 months ago

@SutuSebastian I tried to use custom CSS classes that include @apply rules too but they failed to override default styles. So I used the theme prop instead, but this way I have to set styles like colour for each component by using theme styles. Is there a way to set the primary colour globally, the default colour is cyan.

SutuSebastian commented 4 months ago

An improved design system config is coming up, there's a lot of work involved and u'll be able to simply set a primary color in the tailwind.config.js file and it will apply in all components.

avishkakavindu commented 4 months ago

Will that be backwards compatible?

SutuSebastian commented 4 months ago

Will that be backwards compatible?

I'll try to keep as much as I can from the old API.

avishkakavindu commented 4 months ago

@SutuSebastian btw is the library stable enough to use in production-grade applications, or do we have to wait for the official release?

SutuSebastian commented 4 months ago

Best option would be to wait for v1.