Skip to content

Commit

Permalink
added a nav_attributes argument to navbars.collapse() + version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
volfpeter committed Oct 24, 2022
1 parent 48e5ad5 commit 67b470a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 49 deletions.
11 changes: 4 additions & 7 deletions markyp_bootstrap4/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@


__author__ = "Peter Volf"
__copyright__ = "Copyright 2019, Peter Volf"
__copyright__ = "Copyright 2022, Peter Volf"
__email__ = "[email protected]"
__license__ = "MIT"
__url__ = "https://github.com/volfpeter/markyp-bootstrap4"
__version__ = "0.1910.0"
__version__ = "0.2210.1"


__all__ = ("CDNs", "req")
Expand Down Expand Up @@ -151,11 +151,7 @@ def all_js(self) -> Tuple[script, script, script]:
"""
# Bootstrap must be the last item on the list (or at least it should come
# after jQuery), otherwise some components won't work.
return (
self.jquery,
self.popper_js,
self.bootstrap_js
)
return (self.jquery, self.popper_js, self.bootstrap_js)

@property
def bootstrap_css(self) -> link:
Expand Down Expand Up @@ -185,6 +181,7 @@ def popper_js(self) -> script:
"""
return script.ref(CDNs.PopperJS.js_url())


req: __BootstrapRequirements = __BootstrapRequirements()
"""
Components that are required in a page to make Bootstrap fully functional.
Expand Down
74 changes: 32 additions & 42 deletions markyp_bootstrap4/navbars.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
See https://getbootstrap.com/docs/4.0/components/navbar/.
"""

from typing import Optional, Tuple, Type
from typing import Dict, Optional, Type

from markyp import ElementType, PropertyValue, elements
from markyp_html import block, forms, inline, join, text


__all__ = (
"navbar_text", "ExpandPoint", "Theme", "brand",
"collapse", "navbar", "navbar_nav", "navbar_toggler"
)
__all__ = ("navbar_text", "ExpandPoint", "Theme", "brand", "collapse", "navbar", "navbar_nav", "navbar_toggler")


navbar_text: text.StyledTextFactory = text.StyledTextFactory("navbar-text")
Expand Down Expand Up @@ -48,11 +45,7 @@ class Theme(object):
LIGHT = "navbar-light"



def brand(*args: ElementType,
class_: Optional[str] = None,
href: str = "#",
**kwargs: PropertyValue) -> inline.a:
def brand(*args: ElementType, class_: Optional[str] = None, href: str = "#", **kwargs: PropertyValue) -> inline.a:
"""
Creates an anchor element with `navbar-brand` style.
Expand All @@ -68,11 +61,14 @@ def brand(*args: ElementType,
return inline.a(*args, href=href, class_=join("navbar-brand", class_), **kwargs)


def collapse(*args: ElementType,
id: str,
class_: Optional[str] = None,
nav_factory: Optional[elements.Element] = None,
**kwargs: PropertyValue) -> block.div:
def collapse(
*args: ElementType,
id: str,
class_: Optional[str] = None,
nav_factory: Optional[elements.Element] = None,
nav_attributes: Optional[Dict[str, PropertyValue]] = None,
**kwargs: PropertyValue,
) -> block.div:
"""
Creates a `navbar-collapse` element using the provided factory type.
Expand All @@ -90,20 +86,24 @@ def collapse(*args: ElementType,
nav_factory: An optional factory type to create a `navbar_nav` wrapper around
the positional arguments given to the method. If `None`, then the
positional arguments will not be wrapped in any way.
nav_attributes: Attributes (keyword arguments) that should be passed on to the
`navbar_nav` component.
"""
return block.div(
*[navbar_nav(*args, factory=nav_factory)] if nav_factory is not None else args,
*[navbar_nav(*args, **(nav_attributes or {}), factory=nav_factory)] if nav_factory is not None else args,
class_=join("collapse navbar-collapse", class_),
id=id,
**kwargs
**kwargs,
)


def navbar(*args: ElementType,
class_: Optional[str] = None,
expand_point: Optional[str] = None,
theme: Optional[str] = None,
**kwargs: PropertyValue) -> block.nav:
def navbar(
*args: ElementType,
class_: Optional[str] = None,
expand_point: Optional[str] = None,
theme: Optional[str] = None,
**kwargs: PropertyValue,
) -> block.nav:
"""
Creates a `navbar` element.
Expand All @@ -119,22 +119,15 @@ def navbar(*args: ElementType,
theme: The theme of the navbar. It must be one of the constants from the
`Theme` class or `None`.
"""
return block.nav(
*args,
class_=join(
"navbar",
expand_point,
theme,
class_
),
**kwargs
)
return block.nav(*args, class_=join("navbar", expand_point, theme, class_), **kwargs)


def navbar_nav(*args: ElementType,
class_: Optional[str] = None,
factory: Type[elements.Element] = block.div,
**kwargs: PropertyValue) -> elements.Element:
def navbar_nav(
*args: ElementType,
class_: Optional[str] = None,
factory: Type[elements.Element] = block.div,
**kwargs: PropertyValue,
) -> elements.Element:
"""
Creates a `navbar-nav` element using the given factory.
Expand All @@ -150,10 +143,7 @@ def navbar_nav(*args: ElementType,
return factory(*args, class_=join("navbar-nav", class_), **kwargs)


def navbar_toggler(*,
collapse_id: str,
class_: Optional[str] = None,
**kwargs: PropertyValue) -> forms.button:
def navbar_toggler(*, collapse_id: str, class_: Optional[str] = None, **kwargs: PropertyValue) -> forms.button:
"""
Creates a `navbar-toggler` button.
Expand All @@ -174,6 +164,6 @@ def navbar_toggler(*,
"data-target": f"#{collapse_id}",
"aria-controls": collapse_id,
"aria-expanded": False,
"aria-label": "Toggle navigation"
}
"aria-label": "Toggle navigation",
},
)

0 comments on commit 67b470a

Please sign in to comment.