mirrored from git://git.moodle.org/moodle.git
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-78204 core: Add new showmore/showless UI component
- Create a new 'showmore' template that receives both collapsed and expanded content. Initially only the collapsed content will be displayed with a "Show more" button. When it is expanded, only the expanded content will be displayed with "Show less" button. - Add 'showmore' component to component library
- Loading branch information
Showing
11 changed files
with
281 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
admin/tool/componentlibrary/content/moodle/components/showmore.md
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,40 @@ | ||
--- | ||
layout: docs | ||
title: "Show more" | ||
date: 2023-06-12T00:00:00+01:00 | ||
draft: false | ||
weight: 70 | ||
tags: | ||
- MDL-78204 | ||
- 4.3 | ||
--- | ||
|
||
## How to use | ||
|
||
The show more component is used to show and hide content. It is useful for showing a preview of content and then allowing the user to expand it to see more. | ||
|
||
The parameters for the template context are: | ||
* collapsedcontent: The content to show when collapsed. | ||
* expandedcontent: The content to show when expanded. | ||
* extraclasses: Any extra classes added to the showmore outer container. | ||
* buttonextraclasses: Any extra classes added to the button. | ||
* collapsedextraclasses: Any extra classes added to the collapsed content container. | ||
* expandedextraclasses: Any extra classes added to the expanded content container. | ||
|
||
## Example | ||
|
||
{{< mustache template="core/showmore" >}} | ||
{ | ||
"collapsedcontent": "Hello...", | ||
"expandedcontent": "Hello<br>Is it me you're looking for? I can see it in your eyes", | ||
"extraclasses": "rounded p-2 border", | ||
"buttonextraclasses": "font-weight-bold" | ||
} | ||
{{< /mustache >}} | ||
|
||
## Example used as a template block | ||
|
||
{{< mustache template="tool_componentlibrary/examples/showmore/example" >}} | ||
{ | ||
} | ||
{{< /mustache >}} |
37 changes: 37 additions & 0 deletions
37
admin/tool/componentlibrary/templates/examples/showmore/example.mustache
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,37 @@ | ||
{{! | ||
This file is part of Moodle - http://moodle.org/ | ||
Moodle is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
Moodle is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
}} | ||
{{! | ||
@template tool_componentlibrary/examples/showmore/example | ||
Template for showmore element used as a block example | ||
Example context (json): | ||
{ | ||
} | ||
}} | ||
<div id="showmore-example"> | ||
<h1>Show more block</h1> | ||
<p>This is a simple example using showmore template as a block.</p> | ||
{{< core/showmore }} | ||
{{$collapsedcontent}} Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem... {{/collapsedcontent}} | ||
{{$expandedcontent}} | ||
Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem suspendisse faucibus, nunc et pellentesque egestas, lacus ante convallis tellus, vitae iaculis lacus elit id tortor. | ||
Sed augue ipsum, egestas nec, vestibulum et, malesuada adipiscing, dui. Sed cursus turpis vitae tortor. Praesent turpis. Proin viverra, ligula sit amet ultrices semper, ligula arcu | ||
tristique sapien, a accumsan nisi mauris ac eros. Donec venenatis vulputate lorem. Vestibulum eu odio. Morbi nec metus. Vivamus euismod mauris. Vivamus elementum semper nisi. | ||
{{/expandedcontent}} | ||
{{/core/showmore }} | ||
</div> |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* Initializes and handles events fow 'showmore' components. | ||
* | ||
* @module core/showmore | ||
* @copyright 2023 Mikel Martín <mikel@moodle.com> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
const Selectors = { | ||
actions: { | ||
toggleContent: '[data-action="toggle-content"]' | ||
}, | ||
regions: { | ||
main: '[data-region="showmore"]' | ||
} | ||
}; | ||
|
||
let initialized = false; | ||
|
||
/** | ||
* Initialise module | ||
* | ||
* @method | ||
*/ | ||
export const init = () => { | ||
|
||
if (initialized) { | ||
// We already added the event listeners (can be called multiple times by mustache template). | ||
return; | ||
} | ||
|
||
// Listen for click events. | ||
document.addEventListener('click', (event) => { | ||
const toggleContent = event.target.closest(Selectors.actions.toggleContent); | ||
if (toggleContent) { | ||
const region = toggleContent.closest(Selectors.regions.main); | ||
region.classList.toggle('collapsed'); | ||
const toggleButton = region.querySelector(Selectors.actions.toggleContent); | ||
toggleButton.setAttribute('aria-expanded', !region.classList.contains('collapsed')); | ||
} | ||
}); | ||
|
||
initialized = true; | ||
}; |
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,52 @@ | ||
{{! | ||
This file is part of Moodle - http://moodle.org/ | ||
Moodle is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
Moodle is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
}} | ||
{{! | ||
@template core/showmore | ||
Displays content with 'Show more'/'Show less' feature. | ||
Example context (json): | ||
{ | ||
"collapsedcontent": "Hello...", | ||
"expandedcontent": "Hello...<br>Is it me you're looking for? I can see it in your eyes", | ||
"extraclasses": "p-3", | ||
"collapsedextraclasses": "bg-secondary", | ||
"expandedextraclasses": "bg-secondary" | ||
} | ||
}} | ||
<div class="showmore-container collapsed {{$extraclasses}}{{extraclasses}}{{/extraclasses}}" data-region="showmore"> | ||
<button | ||
type="button" | ||
class="btn btn-sm btn-link p-0 {{$buttonextraclasses}}{{buttonextraclasses}}{{/buttonextraclasses}}" | ||
aria-expanded="false" | ||
data-action="toggle-content" | ||
> | ||
<span class="collapsed-content"> {{#str}} showmore, core {{/str}} {{#pix}} t/expandedchevron, core {{/pix}} </span> | ||
<span class="expanded-content"> {{#str}} showless, core {{/str}} {{#pix}} t/collapsedchevron_up, core {{/pix}} </span> | ||
</button> | ||
<div class="collapsed-content {{$collapsedextraclasses}}{{collapsedextraclasses}}{{/collapsedextraclasses}}"> | ||
{{$collapsedcontent}}{{{collapsedcontent}}}{{/collapsedcontent}} | ||
</div> | ||
<div class="expanded-content {{$expandedextraclasses}}{{expandedextraclasses}}{{/expandedextraclasses}}"> | ||
{{$expandedcontent}}{{{expandedcontent}}}{{/expandedcontent}} | ||
</div> | ||
</div> | ||
{{#js}} | ||
require(['core/showmore'], function(showmore) { | ||
showmore.init(); | ||
}); | ||
{{/js}} |
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