Skip to content

Commit 3ff8f64

Browse files
authored
Merge pull request #339 from PanderMusubi/icon_improvement
reduced whitespace and support for extra classes for icons
1 parent 658a81b commit 3ff8f64

File tree

6 files changed

+25
-14
lines changed

6 files changed

+25
-14
lines changed

CHANGES.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Changelog
22
=========
33

4+
- Reduced icon whitespace and support for classes.
45
- Upgrade to Bootstrap Icons 1.11.3.
56

67
2.4.1

docs/macros.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ Example
621621
API
622622
~~~~
623623

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

examples/bootstrap4/templates/icon.html

+4
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

+4
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

+9-9
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

+5-4
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ def icon_title():
3737
{{ render_icon('heart', title='Heart') }}
3838
''')
3939

40-
@app.route('/icon-desc')
41-
def icon_desc():
40+
@app.route('/icon-desc-classes')
41+
def icon_desc_classes():
4242
return render_template_string('''
4343
{% from 'bootstrap4/utils.html' import render_icon %}
44-
{{ render_icon('heart', desc='A heart.') }}
44+
{{ render_icon('heart', desc='A heart.', classes='text-success bg-light') }}
4545
''')
4646

4747
response = client.get('/icon')
@@ -71,10 +71,11 @@ def icon_desc():
7171
assert 'bootstrap-icons.svg#heart' in data
7272
assert '<title>Heart</title>' in data
7373

74-
response = client.get('/icon-desc')
74+
response = client.get('/icon-desc-classes')
7575
data = response.get_data(as_text=True)
7676
assert 'bootstrap-icons.svg#heart' in data
7777
assert '<desc>A heart.</desc>' in data
78+
assert 'class="bi text-success bg-light"' in data
7879

7980

8081
def test_render_icon_config(app, client):

0 commit comments

Comments
 (0)