-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement and migrate to a shortcode-controlled, tabbed interface (#1…
…0793) Introduces new `{% tabs "save-key" %}` and `{% tab "name" %}` 11ty/Liquid paired shortcodes to add tabs to the site and switches the various tab methods across the site (`codesample` and raw HTML) to use them. The `save-key` parameter is optional and allows syncing of tabs across the site to the same tab. For example, the `darwin-language` key is used to switch all tabs with the same key to either `Swift` or `Objective-C`. The `name` parameter tab is not optional. Both `save-key` and `name` must be in quotes or refer to a Liquid/11ty variable that is a string. The `{% endtab %}` and `{% endtabs %}` closing shortcodes are required to close a tab and a tab menu respectively. **Example:** ````liquid {% tabs "darwin-language" %} {% tab "Swift" %} Some words about the following Swift snippet... ```swift let flutterViewController = FlutterViewController( project: nil, initialRoute: "/onboarding", nibName: nil, bundle: nil) ``` {% endtab %} {% tab "Objective-C" %} Some words about the following Objective-C snippet... ```objc FlutterViewController* flutterViewController = [[FlutterViewController alloc] initWithProject:nil initialRoute:@"/onboarding" nibName:nil bundle:nil]; ``` {% endtab %} {% endtabs %} ```` Resolves #8926 Contributes to #8889 Contributes to #8030
- Loading branch information
Showing
38 changed files
with
505 additions
and
810 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import {slugify} from './utils/slugify.js'; | ||
|
||
export function registerShortcodes(eleventyConfig) { | ||
_setupTabs(eleventyConfig); | ||
} | ||
|
||
function _setupTabs(eleventyConfig) { | ||
// Variable shared between all tabs to ensure each has a unique ID. | ||
let currentTabId = 0; | ||
|
||
// Variables that are shared between the tabs within a tabs shortcode, | ||
// and should be reset before returning from tabs. | ||
let tabs = []; | ||
let markTabAsActive = true; | ||
|
||
eleventyConfig.addPairedShortcode('tabs', function (content, saveKey) { | ||
let tabMarkup = `<div class="tabs-wrapper" ${saveKey ? `data-tab-save-key="${slugify(saveKey)}"` : ''}><ul class="nav nav-tabs" role="tablist">`; | ||
let activeTab = true; | ||
|
||
tabs.forEach((tab) => { | ||
const tabName = tab.name; | ||
const tabSaveId = slugify(tabName); | ||
const tabId = `${tab.id}-tab`; | ||
const tabPanelId = `${tabId}-panel`; | ||
tabMarkup += `<li class="nav-item"> | ||
<a class="nav-link ${activeTab ? "active" : ""}" data-tab-save-id="${tabSaveId}" id="${tabId}" href="#${tabPanelId}" role="tab" aria-controls="${tabPanelId}" aria-selected="${activeTab ? "true" : "false"}">${tabName}</a> | ||
</li>`; | ||
activeTab = false; | ||
}); | ||
|
||
tabMarkup += '</ul><div class="tab-content">\n'; | ||
tabMarkup += content; | ||
tabMarkup += '\n</div></div>'; | ||
|
||
// Reset shared variables. | ||
tabs = []; | ||
markTabAsActive = true; | ||
|
||
return tabMarkup; | ||
}); | ||
|
||
eleventyConfig.addPairedShortcode('tab', function (content, tabName) { | ||
const tabIdNumber = currentTabId++; | ||
tabs.push({name: tabName, id: tabIdNumber}); | ||
const tabId = `${tabIdNumber}-tab`; | ||
const tabPanelId = `${tabId}-panel`; | ||
const tabContent = `<div class="tab-pane ${markTabAsActive ? "active" : ""}" id="${tabPanelId}" role="tabpanel" aria-labelledby="${tabId}"> | ||
${content} | ||
</div> | ||
`; | ||
|
||
// No other tabs should be marked as active. | ||
markTabAsActive = false; | ||
|
||
return tabContent; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.