Skip to content

Commit

Permalink
Improve handling of responsive layouts (#4491)
Browse files Browse the repository at this point in the history
* Card cleanup

* Add overflow-x on non-root responsive layouts

* Make card header sticky

* Update tests
  • Loading branch information
philippjfr authored Mar 2, 2023
1 parent f4395e5 commit 62fe471
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 45 deletions.
58 changes: 33 additions & 25 deletions panel/dist/css/card.css
Original file line number Diff line number Diff line change
@@ -1,48 +1,56 @@
:host(.card) {
border: 1px solid rgba(0,0,0,.125);
border-radius: 0.25rem;
border: 1px solid rgba(0,0,0,.125);
border-radius: 0.25rem;
}

:host(.accordion) {
border: 1px solid rgba(0,0,0,.125);
border: 1px solid rgba(0,0,0,.125);
}

.card-header {
align-items: center;
background-color: rgba(0, 0, 0, 0.03);
border-radius: 0.25rem;
display: inline-flex;
justify-content: start;
width: 100%;
align-items: center;
background-color: rgba(0, 0, 0, 0.03);
border: 1px solid;
border-radius: 0.25rem;
display: inline-flex;
justify-content: start;
position: sticky;
left: 0;
width: 100%;
}

.accordion-header {
align-items: center;
background-color: rgba(0, 0, 0, 0.03);
border-radius: 0;
display: flex;
justify-content: start;
width: 100%;
align-items: center;
background-color: rgba(0, 0, 0, 0.03);
border: 1px solid;
border-radius: 0;
display: flex;
justify-content: start;
position: sticky;
left: 0;
width: 100%;
}

.card-button {
background-color: transparent;
margin-left: 0.5em;
margin-right: 0.5em;
background-color: transparent;
margin-left: 0.5em;
margin-right: 0.5em;
}

.card-header-row {
position: relative !important;
margin-bottom: auto;
margin-top: auto;
position: relative !important;
}

.card-title {
align-items: center;
font-size: 1.4em;
font-weight: bold;
overflow-wrap: break-word;
align-items: center;
font-size: 1.4em;
font-weight: bold;
overflow-wrap: break-word;
}

.card-header-row > .bk {
overflow-wrap: break-word;
text-align: center;
overflow-wrap: break-word;
text-align: center;
}
23 changes: 18 additions & 5 deletions panel/layout/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,10 @@ def __repr__(self, depth: int = 0, max_depth: int = 10) -> str:
# Callback API
#----------------------------------------------------------------

def _init_params(self) -> Dict[str, Any]:
return {
p: v for p, v in self.param.values().items()
if v is not None and p != 'objects'
}
def _process_param_change(self, params: Dict[str, Any]) -> Dict[str, Any]:
if ('styles' in params or 'sizing_mode' in params) and self.sizing_mode in ('stretch_width', 'stretch_both'):
params['styles'] = dict(params.get('styles', {}), **{'overflow-x': 'auto'})
return super()._process_param_change(params)

def _update_model(
self, events: Dict[str, param.parameterized.Event], msg: Dict[str, Any],
Expand All @@ -86,6 +85,10 @@ def _update_model(
inverse = {v: k for k, v in self._property_mapping.items() if v is not None}
preprocess = any(inverse.get(k, k) in self._preprocess_params for k in msg)

# ALERT: Find a better way to handle this
if 'styles' in msg and root is model and 'overflow-x' in msg['styles']:
del msg['styles']['overflow-x']

obj_key = self._property_mapping['objects']
if obj_key in msg:
old = events['objects'].old
Expand Down Expand Up @@ -159,6 +162,16 @@ def _get_model(
# Public API
#----------------------------------------------------------------

def get_root(
self, doc: Optional[Document] = None, comm: Optional[Comm] = None,
preprocess: bool = True
) -> Model:
root = super().get_root(doc, comm, preprocess)
# ALERT: Find a better way to handle this
if hasattr(root, 'styles') and 'overflow-x' in root.styles:
del root.styles['overflow-x']
return root

def select(self, selector=None):
"""
Iterates over the Viewable and any potential children in the
Expand Down
2 changes: 1 addition & 1 deletion panel/layout/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def _update_header(self, *events):
from ..pane import HTML, panel
if self.header is None:
params = {
'object': '%s' % (self.title or "​"),
'object': f'<h3>{self.title}</h3>' if self.title else "&#8203;",
'css_classes': self.title_css_classes,
'margin': (5, 0)
}
Expand Down
14 changes: 8 additions & 6 deletions panel/tests/layout/test_accordion.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ def test_accordion_constructor(document, comm):
assert all(isinstance(c, Card) for c in model.children)
card1, card2 = model.children

assert card1.children[0].children[0].text == 'Div1'
assert card1.children[0].children[0].text == '&lt;h3&gt;Div1&lt;/h3&gt;'
assert card1.children[1] is div1
assert card2.children[0].children[0].text == 'Div2'
assert card2.children[0].children[0].text == '&lt;h3&gt;Div2&lt;/h3&gt;'
assert card2.children[1] is div2


Expand All @@ -69,9 +69,11 @@ def test_accordion_implicit_constructor(document, comm):
assert all(isinstance(c, Card) for c in model.children)
card1, card2 = model.children

assert card1.children[0].children[0].text == p1.name == 'Div1'
assert p1.name == 'Div1'
assert card1.children[0].children[0].text == '&lt;h3&gt;Div1&lt;/h3&gt;'
assert card1.children[1] is div1
assert card2.children[0].children[0].text == p2.name == 'Div2'
assert p2.name == 'Div2'
assert card2.children[0].children[0].text == '&lt;h3&gt;Div2&lt;/h3&gt;'
assert card2.children[1] is div2


Expand All @@ -88,9 +90,9 @@ def test_accordion_constructor_with_named_objects(document, comm):
assert all(isinstance(c, Card) for c in model.children)
card1, card2 = model.children

assert card1.children[0].children[0].text == 'Tab1'
assert card1.children[0].children[0].text == '&lt;h3&gt;Tab1&lt;/h3&gt;'
assert card1.children[1] is div1
assert card2.children[0].children[0].text == 'Tab2'
assert card2.children[0].children[0].text == '&lt;h3&gt;Tab2&lt;/h3&gt;'
assert card2.children[1] is div2


Expand Down
4 changes: 2 additions & 2 deletions panel/tests/layout/test_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ def test_card_get_root_title(document, comm):

assert isinstance(model, CardModel)
assert model.children == [header, div1, div2]
assert header.children[0].text == "Test"
assert header.children[0].text == '&lt;h3&gt;Test&lt;/h3&gt;'

div3 = Div()
layout.header = div3
assert header.children[0] is div3

layout.header = None
assert header.children[0].text == "Test"
assert header.children[0].text == '&lt;h3&gt;Test&lt;/h3&gt;'


def test_card_get_root_header(document, comm):
Expand Down
4 changes: 2 additions & 2 deletions panel/theme/css/bootstrap.css
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ button.accordion-header {

:host(.card-title) h1, :host(.card-title) h2, :host(.card-title) h3,
:host(.card-title) h4, :host(.card-title) h5, :host(.card-title) h6 {
margin-block-end: 0;
margin-block-start: 0;
margin-block-end: 0.2em;
margin-block-start: 0.2em;
}

/* Tabs styling */
Expand Down
4 changes: 2 additions & 2 deletions panel/theme/css/fast.css
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ canvas, img {

:host(.card-title) h1, :host(.card-title) h2, :host(.card-title) h3,
:host(.card-title) h4, :host(.card-title) h5, :host(.card-title) h6 {
margin-block-end: 0;
margin-block-start: 0;
margin-block-end: 0.2em;
margin-block-start: 0.2em;
}

/* Tabs */
Expand Down
4 changes: 2 additions & 2 deletions panel/theme/css/material.css
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ button.mdc-button.mdc-card-button {

:host(.card-title) h1, :host(.card-title) h2, :host(.card-title) h3,
:host(.card-title) h4, :host(.card-title) h5, :host(.card-title) h6 {
margin-block-end: 0;
margin-block-start: 0;
margin-block-end: 0.2em;
margin-block-start: 0.2em;
}

/* Tabs styling */
Expand Down
6 changes: 6 additions & 0 deletions panel/theme/css/native.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@
color: var(--surface-text-color);
background-color: var(--surface-color);
}

:host(.card-title) h1, :host(.card-title) h2, :host(.card-title) h3,
:host(.card-title) h4, :host(.card-title) h5, :host(.card-title) h6 {
margin-block-end: 0.2em;
margin-block-start: 0.2em;
}

0 comments on commit 62fe471

Please sign in to comment.