Skip to content

Commit 4db2811

Browse files
committed
Show the modal closer to the sidebar and the content
1 parent 8963e6f commit 4db2811

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

hoverxref/_static/css/micromodal.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@
77
bottom: 0;
88
background: rgba(0,0,0,0.6);
99
display: flex;
10-
justify-content: center;
1110
align-items: center;
1211
z-index: 250;
1312
}
1413

1514
.modal__container {
1615
background-color: #fff;
1716
padding: 30px;
18-
max-width: 60%;
17+
max-width: 1000px;
1918
max-height: 85%;
2019
border-radius: 4px;
2120
overflow-y: auto;
2221
box-sizing: border-box;
2322
z-index: 250;
23+
position: relative;
2424
}
2525

2626
.modal__header {

hoverxref/_static/js/hoverxref.js_t

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -124,25 +124,28 @@ $(document).ready(function() {
124124
`
125125
$('body').append(modalHtml);
126126

127-
var delay = {{ hoverxref_modal_hover_delay }}, setTimeoutConst;
128-
$('.hoverxref').hover(function(event) {
129-
var parent = $(event.target).parent();
130-
console.debug('Event: ' + event + ' Element: ' + parent);
131-
event.preventDefault();
132-
133-
setTimeoutConst = setTimeout(function(){
134-
showModal(parent);
135-
}, delay);
136-
}, function(){
137-
clearTimeout(setTimeoutConst);
138-
});
127+
function onShow(modal, element) {
128+
// This is a HACK to get some "smart" left position of the
129+
// modal depending its size.
130+
var container = $('#micromodal .modal__container')
131+
// FIXME: this element only exists in sphinx-rtd-theme
132+
var maxWidth = $('.wy-nav-content').innerWidth() - 150 || 650;
133+
var contentLeft = $('.rst-content:first').position().left || 250;
134+
if (container.width() >= maxWidth) {
135+
var left = contentLeft - 150;
136+
}
137+
else {
138+
var left = contentLeft + 150;
139+
}
140+
console.debug('Container left position: ' + left);
141+
container.css('left', left);
142+
}
139143

140144
function showModal(element) {
141-
var $origin = $(element);
142-
var project = $origin.data('project');
143-
var version = $origin.data('version');
144-
var doc = $origin.data('doc');
145-
var section = $origin.data('section');
145+
var project = element.data('project');
146+
var version = element.data('version');
147+
var doc = element.data('doc');
148+
var section = element.data('section');
146149
console.debug('Data: project=' + project + ' version=' + version + ' doc=' + doc + ' section=' + section);
147150
var url = '{{ hoverxref_tooltip_api_host }}' + '/api/v2/embed/?' + 'project=' + project + '&version=' + version + '&doc=' + doc + '&section=' + section;
148151

@@ -164,6 +167,7 @@ $(document).ready(function() {
164167
$('#micromodal-title').html(a);
165168
$('#micromodal-content').html(content);
166169
MicroModal.show('micromodal', {
170+
onShow: {{ hoverxref_modal_onshow_function|default('onShow', true) }},
167171
openClass: '{{ hoverxref_modal_openclass }}',
168172
disableScroll: {{ 'true' if hoverxref_modal_disable_scroll else 'false' }},
169173
disableFocus: {{ 'true' if hoverxref_modal_disable_focus else 'false' }},
@@ -182,5 +186,18 @@ $(document).ready(function() {
182186
};
183187
});
184188
};
189+
190+
var delay = {{ hoverxref_modal_hover_delay }}, setTimeoutConst;
191+
$('.hoverxref').hover(function(event) {
192+
var element = $(this);
193+
console.debug('Event: ' + event + ' Element: ' + element);
194+
event.preventDefault();
195+
196+
setTimeoutConst = setTimeout(function(){
197+
showModal(element);
198+
}, delay);
199+
}, function(){
200+
clearTimeout(setTimeoutConst);
201+
});
185202
{% endif %}
186203
});

hoverxref/extension.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ def setup(app):
125125
app.add_config_value('hoverxref_modal', False, 'env')
126126
app.add_config_value('hoverxref_modal_hover_delay', 350, 'env')
127127
app.add_config_value('hoverxref_modal_class', 'rst-content', 'env')
128+
app.add_config_value('hoverxref_modal_onshow_function', None, 'env')
128129
app.add_config_value('hoverxref_modal_openclass', 'is-open', 'env')
129130
app.add_config_value('hoverxref_modal_disable_focus', True, 'env')
130131
app.add_config_value('hoverxref_modal_disable_scroll', False, 'env')

0 commit comments

Comments
 (0)