{"id":1406,"date":"2023-09-05T05:00:00","date_gmt":"2023-09-05T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1406"},"modified":"2023-09-07T14:33:40","modified_gmt":"2023-09-07T21:33:40","slug":"using-the-angular-material-slider","status":"publish","type":"post","link":"http:\/\/www.angulartraining.com\/daily-newsletter\/using-the-angular-material-slider\/","title":{"rendered":"Using the Angular Material Slider"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>This is the second guest post by Duncan Faulkner. You can read his first post on <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/getting-started-with-angular-material\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Angular Material here<\/a>. This is the second post in this series.<\/p>\n<\/blockquote>\n\n\n\n<p>The <code><a href=\"https:\/\/material.angular.io\/components\/slider\/overview\" target=\"_blank\" rel=\"noopener\" title=\"\">mat-slider<\/a><\/code> was recently rewritten in Angular 15 as part of a refactoring towards MDC (Material Design Components for the Web). The most notable change to the <code>mat-slider<\/code> is the element now requires up to two <code>input<\/code> elements, instead of just a <code>mat-slider<\/code> element, this allows us to use the slider as either a single slider (with one <code>input<\/code>) or a range (with two <code>inputs<\/code>).<\/p>\n\n\n\n<p>The original <code>mat-slider<\/code> looked like this in versions prior to Angular Material 15:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-comment\">&lt;!-- original --&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">mat-slider<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">mat-slider<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And in versions after 15:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-comment\">&lt;!-- single slider --&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">mat-slider<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">matSliderThumb<\/span> \/&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">mat-slider<\/span>&gt;<\/span>\n\n<span class=\"hljs-comment\">&lt;!-- range slider --&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">mat-slider<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">matSliderStartThumb<\/span> \/&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">matSliderEndThumb<\/span> \/&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">mat-slider<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Slider usage options<\/h2>\n\n\n\n<p>It&#8217;s also possible to set the range slider to have a min\/max or both, for example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-comment\">&lt;!-- range slider --&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">mat-slider<\/span> &#91;<span class=\"hljs-attr\">min<\/span>]=<span class=\"hljs-string\">\"30\"<\/span> &#91;<span class=\"hljs-attr\">max<\/span>]=<span class=\"hljs-string\">\"75\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">matSliderStartThumb<\/span> \/&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">matSliderEndThumb<\/span> \/&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">mat-slider<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If <code>min<\/code> is not supplied then zero is assumed, likewise if <code>max<\/code> is not supplied then one hundred is assumed. If <code>step<\/code> is supplied then <code>max<\/code>is required otherwise the slider will not be able to calculate the step division.<\/p>\n\n\n\n<p>The <code>thumbLabel<\/code> directive has now been replaced with a new <code>discrete<\/code> attribute, this controls whether the value indicator tool-tip is shown when the slider is dragged.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-comment\">&lt;!-- original --&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">mat-slider<\/span> <span class=\"hljs-attr\">thumbLabel<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">mat-slider<\/span>&gt;<\/span>\n\n<span class=\"hljs-comment\">&lt;!-- replaced with --&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">mat-slider<\/span> <span class=\"hljs-attr\">discrete<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">matSliderThumb<\/span> \/&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">mat-slider<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To show the tick-marks on the slider add the <code>showTickMarks<\/code> attribute.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">mat-slider<\/span> <span class=\"hljs-attr\">showTickMarks<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">matSliderThumb<\/span> \/&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">mat-slider<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For now the <code>tickInterval<\/code> property has been removed from the API, though this is being reviewed and could be reintroduced in the future. If we want to define the interval of the tick marks we use the <code>step<\/code> property and the tick marks will match the step:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">mat-slider<\/span> <span class=\"hljs-attr\">discrete<\/span> <span class=\"hljs-attr\">showTickMarks<\/span> <span class=\"hljs-attr\">step<\/span>=<span class=\"hljs-string\">\"10\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">matSliderThumb<\/span> \/&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">mat-slider<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>displayValue<\/code> property has also been removed in favour of the new <code>displayWith<\/code> this property controls the text value of the indicator, we need to provide a function for this property where we can manipulate the value to be displayed. We use the <code>min<\/code> property to set the lowest value, the <code>max<\/code> property to the highest value we want the slider to be an set the <code>step<\/code> property to what we want the increment to be.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">mat-slider<\/span> <span class=\"hljs-attr\">min<\/span>=<span class=\"hljs-string\">\"0\"<\/span> <span class=\"hljs-attr\">max<\/span>=<span class=\"hljs-string\">\"100000\"<\/span> <span class=\"hljs-attr\">step<\/span>=<span class=\"hljs-string\">\"1000\"<\/span> &#91;<span class=\"hljs-attr\">displayWith<\/span>]=<span class=\"hljs-string\">\"updateLabelWithFn\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">matSliderThumb<\/span> \/&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">mat-slider<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MySliderComponent<\/span> <\/span>{\n  updateLabelWithFn(value: number): string {\n    <span class=\"hljs-keyword\">return<\/span> value &gt;= <span class=\"hljs-number\">1000<\/span> ? <span class=\"hljs-built_in\">Math<\/span>.round(value \/ <span class=\"hljs-number\">1000<\/span>) + <span class=\"hljs-string\">'k'<\/span> : <span class=\"hljs-string\">`<span class=\"hljs-subst\">${value}<\/span>`<\/span>;\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>updateLabelWithFn<\/code> function takes the value from the slider and [in this instance] if the value is greater than or equal to 1000 then it will round the number and divide it by 1000 and concatenate the letter <code>k<\/code> to the end, this helps to keep the text small and in the thumbLabel.<\/p>\n\n\n\n<p>The <code>valueText<\/code> property has also been removed, we now have two options we can use the input&#8217;s aria-label-valueText or use the <code>displayWith<\/code> property.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-comment\">&lt;!--before Angular 15 --&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">mat-slider<\/span> &#91;<span class=\"hljs-attr\">valueText<\/span>]=<span class=\"hljs-string\">\"someTextValue\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">mat-slider<\/span>&gt;<\/span>\n\n<span class=\"hljs-comment\">&lt;!--after Angular 15 --&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">mat-slider<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> &#91;<span class=\"hljs-attr\">attr.aria-valueText<\/span>]=<span class=\"hljs-string\">\"someTextValue\"<\/span> <span class=\"hljs-attr\">matSliderThumb<\/span> \/&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">mat-slider<\/span>&gt;<\/span>\n\n<span class=\"hljs-comment\">&lt;!--after Angular 15 --&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">mat-slider<\/span> &#91;<span class=\"hljs-attr\">displayWith<\/span>]=<span class=\"hljs-string\">\"displaySomeTextWithFn\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">matSliderThumb<\/span> \/&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">mat-slider<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>With the rewrite the API of the slider has also changed and has introduced two new components the <code>MatSliderThumb<\/code> and <code>MatSliderRangeThumb<\/code> and provide the following properties:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>@Input() value<\/code>: number<\/li>\n\n\n\n<li><code>@Output() valueChange<\/code>: EventEmitter<\/li>\n\n\n\n<li><code>@Output() dragEnd<\/code>: EventEmitter<\/li>\n\n\n\n<li><code>@Output() dragStart<\/code>: EventEmitter<\/li>\n\n\n\n<li><code>@Input() percentage<\/code>: number<\/li>\n<\/ul>\n\n\n\n<p>And the following methods:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>blur<\/li>\n\n\n\n<li>focus<\/li>\n<\/ul>\n\n\n\n<p>There are two notable absences from the <code>mat-slider<\/code> and these are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>invert &#8211; this reversed the start and end of the slider<\/li>\n\n\n\n<li>vertical &#8211; this rotated the slider 90\u00b0, you could also invert a vertical slider<\/li>\n<\/ul>\n\n\n\n<p>These have been removed as they are not part of the Material Design Specification for the Web (MDC).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Changing the color of a slider<\/h2>\n\n\n\n<p>Like all Angular Material components we can change the color of a <code>mat-slider<\/code> using the <code>color<\/code> property.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">mat-slider<\/span> &#91;<span class=\"hljs-attr\">color<\/span>]=<span class=\"hljs-string\">\"primary\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">matThumbSlider<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">mat-slider<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><\/p>\n\n\n\n<p>In our next post, we&#8217;ll see how to migrate from Angular Material 14 to 15+.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the second guest post by Duncan Faulkner. You can read his first post on Angular Material here. This is the second post in this series. The mat-slider was recently rewritten in Angular 15 as part of a refactoring towards MDC (Material Design Components for the Web). The most notable change to the mat-slider [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,35],"tags":[],"class_list":["post-1406","post","type-post","status-publish","format-standard","hentry","category-angular","category-material"],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1406","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=1406"}],"version-history":[{"count":4,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1406\/revisions"}],"predecessor-version":[{"id":1440,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1406\/revisions\/1440"}],"wp:attachment":[{"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1406"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}