Skip to content

Commit f3894a5

Browse files
committed
reduced whitespace and support for extra classes for icons
1 parent 35c12a8 commit f3894a5

File tree

6 files changed

+34
-11
lines changed

6 files changed

+34
-11
lines changed

CHANGES.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Changelog
22
=========
33

4+
- Reduced icon whitespace and support for classes.
5+
46
2.3.3
57
-----
68

@@ -334,4 +336,4 @@ Release date: 2018/7/1
334336

335337
Release date: 2018/6/11
336338

337-
Initial release.
339+
Initial release.

docs/macros.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ Example
617617
API
618618
~~~~
619619

620-
.. py:function:: render_icon(name, size=config.BOOTSTRAP_ICON_SIZE, color=config.BOOTSTRAP_ICON_COLOR, title=None, desc=None)
620+
.. py:function:: render_icon(name, size=config.BOOTSTRAP_ICON_SIZE, color=config.BOOTSTRAP_ICON_COLOR, title=None, desc=None, classes=None)
621621
622622
:param name: The name of icon, you can find all available names at `Bootstrap Icon <https://icons.getbootstrap.com/>`_.
623623
:param size: The size of icon, you can pass any vaild size value (e.g. ``32``/``'32px'``, ``1.5em``, etc.), default to
@@ -627,3 +627,4 @@ API
627627
string (e.g. ``'red'``, ``'#ddd'`` or ``'(250, 250, 250)'``), default to use configuration ``BOOTSTRAP_ICON_COLOR`` (default value is ``None``).
628628
:param title: The title of the icon for accessibility support.
629629
:param desc: The description of the icon for accessibility support.
630+
:param classes: The classes to use for styling (e.g. ``'text-success bg-body-secondary p-2 rounded-3'``).

examples/bootstrap4/templates/icon.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ <h2>Icon with custom size and custom color</h2>
1818
<pre>{% raw %}{{ render_icon('heart', '2em', 'red') }}{% endraw %}</pre>
1919
Output: {{ render_icon('heart', '2em', 'red') }}
2020

21+
<h2>Icon with additional classes for styling</h2>
22+
<pre>{% raw %}{{ render_icon('heart', '2em', classes='text-success bg-light p-2 rounded-lg') }}{% endraw %}</pre>
23+
Output: {{ render_icon('heart', '4em', classes='text-success bg-light p-2 rounded-lg') }}
24+
2125
<h2>Icon with title and descr</h2>
2226
<pre>{% raw %}{{ render_icon('heart', title='Heart', desc='A heart.') }}{% endraw %}</pre>
2327
Output: {{ render_icon('heart', title='Heart', desc='A heart.') }}

examples/bootstrap5/templates/icon.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ <h2>Icon with custom size and custom color</h2>
1818
<pre>{% raw %}{{ render_icon('heart', '2em', 'red') }}{% endraw %}</pre>
1919
Output: {{ render_icon('heart', '2em', 'red') }}
2020

21+
<h2>Icon with additional classes for styling</h2>
22+
<pre>{% raw %}{{ render_icon('heart', '2em', classes='text-success bg-body-secondary p-2 rounded-3') }}{% endraw %}</pre>
23+
Output: {{ render_icon('heart', '4em', classes='text-success bg-body-secondary p-2 rounded-3') }}
24+
2125
<h2>Icon with title and descr</h2>
2226
<pre>{% raw %}{{ render_icon('heart', title='Heart', desc='A heart.') }}{% endraw %}</pre>
2327
Output: {{ render_icon('heart', title='Heart', desc='A heart.') }}

flask_bootstrap/templates/base/utils.html

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
{% endmacro %}
1111

1212

13-
{% macro render_icon(name, size=config.BOOTSTRAP_ICON_SIZE, color=config.BOOTSTRAP_ICON_COLOR, title=None, desc=None) %}
14-
{% set bootstrap_colors = ['primary', 'secondary', 'success', 'danger', 'warning', 'info', 'light', 'dark', 'muted'] %}
15-
<svg class="bi{% if not color %}"{% elif color in bootstrap_colors %} text-{{ color }}"{% else %}" style="color: {{ color }}"{% endif %}
16-
width="{{ size }}" height="{{ size }}" fill="currentColor">
17-
{% if title is not none %}<title>{{ title }}</title>{% endif %}
18-
{% if desc is not none %}<desc>{{ desc }}</desc>{% endif %}
19-
<use xlink:href="{{ url_for('bootstrap.static', filename='icons/bootstrap-icons.svg') }}#{{ name }}"/>
20-
</svg>
21-
{% endmacro %}
13+
{% macro render_icon(name, size=config.BOOTSTRAP_ICON_SIZE, color=config.BOOTSTRAP_ICON_COLOR, title=None, desc=None, classes=None) %}
14+
{%- set bootstrap_colors = ['primary', 'secondary', 'success', 'danger', 'warning', 'info', 'light', 'dark', 'muted'] -%}
15+
<svg class="bi{% if not color %}{% if classes %} {{ classes }}{% endif %}"
16+
{%- elif color in bootstrap_colors %} text-{{ color }}"{% else %}" style="color: {{ color }}"{% endif -%}
17+
{%- if size %} width="{{ size }}"{% endif %}{% if size %} height="{{ size }}"{% endif %} fill="currentColor">
18+
{%- if title %}<title>{{ title }}</title>{% endif -%}
19+
{%- if desc %}<desc>{{ desc }}</desc>{% endif -%}
20+
<use xlink:href="{{ url_for('bootstrap.static', filename='icons/bootstrap-icons.svg') }}#{{ name }}"/></svg>
21+
{%- endmacro %}
2222

2323

2424
{% macro arg_url_for(endpoint, base) %}

tests/test_bootstrap4/test_render_icon.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ def icon_desc():
4444
{{ render_icon('heart', desc='A heart.') }}
4545
''')
4646

47+
@app.route('/icon-classes')
48+
def icon_classes():
49+
return render_template_string('''
50+
{% from 'bootstrap4/utils.html' import render_icon %}
51+
{{ render_icon('heart', classes='text-success bg-light') }}
52+
''')
53+
4754
response = client.get('/icon')
4855
data = response.get_data(as_text=True)
4956
assert 'bootstrap-icons.svg#heart' in data
@@ -76,6 +83,11 @@ def icon_desc():
7683
assert 'bootstrap-icons.svg#heart' in data
7784
assert '<desc>A heart.</desc>' in data
7885

86+
response = client.get('/icon-classes')
87+
data = response.get_data(as_text=True)
88+
assert 'bootstrap-icons.svg#heart' in data
89+
assert 'class="bi text-success bg-light"' in data
90+
7991

8092
def test_render_icon_config(app, client):
8193
app.config['BOOTSTRAP_ICON_SIZE'] = 100

0 commit comments

Comments
 (0)