Skip to content

Commit b119016

Browse files
committed
Better support for more themes
- sphinx_rtd_theme - alabaster - sphinx_material - typlog
1 parent 4db2811 commit b119016

File tree

6 files changed

+62
-15
lines changed

6 files changed

+62
-15
lines changed

MANIFEST.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ include hoverxref/_static/css/tooltipster.custom.css
88

99
include hoverxref/_static/js/micromodal.min.js
1010
include hoverxref/_static/css/micromodal.css
11-
11+
include hoverxref/_static/css/sphinx-material.css
12+
include hoverxref/_static/css/sphinx_rtd_theme.css

hoverxref/_static/css/micromodal.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
background: rgba(0,0,0,0.6);
99
display: flex;
1010
align-items: center;
11+
justify-content: center;
1112
z-index: 250;
1213
}
1314

@@ -20,7 +21,6 @@
2021
overflow-y: auto;
2122
box-sizing: border-box;
2223
z-index: 250;
23-
position: relative;
2424
}
2525

2626
.modal__header {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#micromodal.md-typeset h1 {
2+
margin: 0;
3+
}
4+
5+
.modal__btn {
6+
font-size: .675rem;
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#micromodal .modal__overlay {
2+
justify-content: unset;
3+
}
4+
5+
#micromodal .modal__container {
6+
position: relative;
7+
}

hoverxref/_static/js/hoverxref.js_t

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,20 +125,21 @@ $(document).ready(function() {
125125
$('body').append(modalHtml);
126126

127127
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;
128+
if (typeof READTHEDOCS_DATA !== 'undefined' && READTHEDOCS_DATA.theme == 'sphinx_rtd_theme') {
129+
// This is a HACK to get some "smart" left position of the
130+
// modal depending its size.
131+
var container = $('#micromodal .modal__container')
132+
var maxWidth = $('.wy-nav-content').innerWidth() - 150;
133+
var contentLeft = $('.wy-nav-content').position().left;
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);
139142
}
140-
console.debug('Container left position: ' + left);
141-
container.css('left', left);
142143
}
143144

144145
function showModal(element) {

hoverxref/extension.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,36 @@ def setup_sphinx_tabs(app, config):
9393
app.disconnect(listener_id)
9494

9595

96+
def setup_theme(app, exception):
97+
"""
98+
Auto-configure default settings for known themes.
99+
100+
Add a small custom CSS file for a specific theme and set hoverxref configs
101+
(if not overwritten by the user) with better defaults for these themes.
102+
"""
103+
css_file = None
104+
theme = app.config.html_theme
105+
default, rebuild, types = app.config.values.get('hoverxref_modal_class')
106+
if theme == 'sphinx_material':
107+
if app.config.hoverxref_modal_class == default:
108+
app.config.hoverxref_modal_class = 'md-typeset'
109+
css_file = 'css/material.css'
110+
elif theme == 'alabaster':
111+
if app.config.hoverxref_modal_class == default:
112+
app.config.hoverxref_modal_class = 'body'
113+
elif theme == 'sphinx_rtd_theme':
114+
if app.config.hoverxref_modal_class == default:
115+
css_file = 'css/sphinx_rtd_theme.css'
116+
117+
if css_file:
118+
app.add_css_file(css_file)
119+
path = os.path.join(os.path.dirname(__file__), '_static', css_file)
120+
copy_asset(
121+
path,
122+
os.path.join(app.outdir, '_static', 'css'),
123+
)
124+
125+
96126
def setup(app):
97127
"""Setup ``hoverxref`` Sphinx extension."""
98128

@@ -144,6 +174,7 @@ def setup(app):
144174

145175
app.connect('config-inited', setup_domains)
146176
app.connect('config-inited', setup_sphinx_tabs)
177+
app.connect('config-inited', setup_theme)
147178
app.connect('build-finished', copy_asset_files)
148179

149180
for f in ASSETS_FILES:

0 commit comments

Comments
 (0)