Jump to content

MediaWiki:Common.js: Difference between revisions

From Yusupov's House
Created page with "Quick facts: collapse infobox under first paragraph on touch devices: mw.hook('wikipage.content').add(function ($content) { // Only on touch devices if (!window.matchMedia || !window.matchMedia('(pointer: coarse)').matches) return; var $ibox = $content.find('table.infobox').first(); if (!$ibox.length) return; // First real paragraph in article content var $firstP = $content .find('.mw-parser-output > p') .filter(function () { return $(this).tex..."
(No difference)

Revision as of 18:28, 2 October 2025

/* Quick facts: collapse infobox under first paragraph on touch devices */
mw.hook('wikipage.content').add(function ($content) {
  // Only on touch devices
  if (!window.matchMedia || !window.matchMedia('(pointer: coarse)').matches) return;

  var $ibox = $content.find('table.infobox').first();
  if (!$ibox.length) return;

  // First real paragraph in article content
  var $firstP = $content
    .find('.mw-parser-output > p')
    .filter(function () { return $(this).text().trim().length > 0; })
    .first();
  if (!$firstP.length) return;

  // Build <details> wrapper
  var $details = $('<details>', { 'class': 'quickfacts', 'aria-label': 'Quick facts' });
  var $summary = $('<summary>', { 'class': 'quickfacts-summary', text: 'Quick facts' });

  $details.append($summary);

  // Mark infobox so CSS can remove float/width inside quickfacts
  $ibox.addClass('infobox--in-quickfacts');

  // Move the infobox inside the details (not cloning; avoids duplicate content for SR)
  $details.append($ibox);

  // Insert directly after the first paragraph
  $details.insertAfter($firstP);
});