42 lines
5.5 KiB
JavaScript
42 lines
5.5 KiB
JavaScript
import{j as e}from"./index-bIIEL2MP.js";import{useMDXComponents as r}from"./index-CO-0pc0F.js";import{M as i}from"./index-Z-6k0Xrj.js";import{p as c}from"./docsPage-CT2vyZOj.js";import{H as t}from"./Highlight-rHebhLku.js";import"./_commonjsHelpers-D6-XlEtG.js";import"./iframe-CzjIX-qr.js";import"./index-aQYXhgXp.js";import"./index-DrFu-skq.js";const a=""+new URL("scss-start-BVE0yKS1.png",import.meta.url).href,h=""+new URL("scss-highlight-DJgSTLTu.png",import.meta.url).href,l=""+new URL("scss-inspector-DX09FcYW.png",import.meta.url).href,d=""+new URL("scss-change-0EQn-a_A.png",import.meta.url).href,p=""+new URL("scss-test-D0OKY1jS.png",import.meta.url).href,m=""+new URL("scss-winning-Bu-D1jUf.png",import.meta.url).href;function o(s){const n={code:"code",em:"em",h1:"h1",h2:"h2",h3:"h3",p:"p",pre:"pre",strong:"strong",...r(),...s.components};return e.jsxs(e.Fragment,{children:[e.jsx(i,{title:"Guides/Customising components with SCSS",parameters:{...c}}),`
|
||
`,e.jsx(n.h1,{id:"customising-components-with-scss",children:"Customising components with SCSS"}),`
|
||
`,e.jsx(n.p,{children:"One of the most powerful ways to customise components isn't props or, even, Svelte."}),`
|
||
`,e.jsx("p",{className:"sbdocs-p",children:e.jsx("span",{className:"highlight bold",children:e.jsx(n.p,{children:"It’s SCSS and your web inspector!"})})}),`
|
||
`,e.jsx(n.h2,{id:"hows-that",children:"How's that??"}),`
|
||
`,e.jsxs(n.p,{children:["Let's say you wanted to change our ",e.jsx(n.code,{children:"BeforeAfter"})," component. You want the text overlays to be at the bottom of the image instead of the top like this:"]}),`
|
||
`,e.jsx("img",{src:a,width:"600"}),`
|
||
`,e.jsxs(n.p,{children:["The first thing you should do is ",e.jsx(t,{children:"check out the elements you want to change in your web inspector"})," and see if CSS can make the change you want."]}),`
|
||
`,e.jsx("img",{src:h,width:"300",style:{margin:"0 0 1rem"}}),`
|
||
`,e.jsx("img",{src:l,width:"100%"}),`
|
||
`,e.jsxs(n.p,{children:["In our case, we want to change the absolute position of those elements. To test that'll actually work, we can ",e.jsx(t,{children:"try it directly in the inspector first!"})]}),`
|
||
`,e.jsx("img",{src:d,width:"500",style:{margin:"0 0 1rem"}}),`
|
||
`,e.jsx("img",{src:p,width:"600",style:{margin:"0 0 1rem"}}),`
|
||
`,e.jsxs(n.p,{children:["Now that we know we can change what we need through CSS it's time to write some SCSS, either in your ",e.jsx(n.code,{children:"global.scss"})," file or directly in a component like ",e.jsx(n.code,{children:"App.svelte"}),"."]}),`
|
||
`,e.jsx(n.p,{children:"First, let's look at the class of the style rule we changed in the inspector:"}),`
|
||
`,e.jsx(n.p,{children:e.jsx(n.code,{children:"figure.before-after-container.s-khJY-w4TYkp5 .overlay-container.before.s-khJY-w4TYkp5"})}),`
|
||
`,e.jsxs(n.p,{children:["One thing we always need to do is ",e.jsx(t,{children:"strip out any Svelte class names"}),", i.e., those weird ",e.jsx(n.code,{children:".s-khJY-w4TYkp5"})," classes. Why? Those are random classes Svelte adds to CSS, and we can't guarantee they won't change."]}),`
|
||
`,e.jsx(n.p,{children:"That leaves us with:"}),`
|
||
`,e.jsx(n.p,{children:e.jsx(n.code,{children:"figure.before-after-container .overlay-container.before"})}),`
|
||
`,e.jsxs(n.p,{children:["But we need our style rule to ",e.jsx(n.em,{children:"beat"})," the original style in the CSS cascade, and right now, it's less specific without those class names we stripped."]}),`
|
||
`,e.jsxs(n.p,{children:["The easiest way to make sure your style rule wins out is to ",e.jsx(t,{children:"add an ID either directly to the element or to a parent"}),". In our case, let's add an ID through the ",e.jsx(n.code,{children:"BeforeAfter"})," ",e.jsx(n.code,{children:"id"})," prop. Now we can use it! (For extra credit, though, we'll drop the ",e.jsx(n.code,{children:".before"})," so our new style rule applies to ",e.jsx(n.em,{children:"both"})," overlays.)"]}),`
|
||
`,e.jsx(n.pre,{children:e.jsx(n.code,{className:"language-scss",children:`figure#my-before-after .overlay-container {
|
||
bottom: 0;
|
||
}
|
||
`})}),`
|
||
`,e.jsx(n.p,{children:"Now our selector is more specific. We win!"}),`
|
||
`,e.jsxs(n.p,{children:["If you don't see a way to add an ID through a component's props, then just wrap the component in a ",e.jsx(n.code,{children:"div"})," in your code:"]}),`
|
||
`,e.jsx(n.pre,{children:e.jsx(n.code,{className:"language-svelte",children:`<div id="my-before-after">
|
||
<BeforeAfter />
|
||
</div>
|
||
`})}),`
|
||
`,e.jsx(n.p,{children:"... and use that, instead!"}),`
|
||
`,e.jsx(n.pre,{children:e.jsx(n.code,{className:"language-scss",children:`div#my-before-after figure .overlay-container {
|
||
bottom: 0;
|
||
}
|
||
`})}),`
|
||
`,e.jsxs(n.p,{children:["We can ",e.jsx(t,{children:"confirm the new style rule is winning by looking at its order back in our web inspector!"})]}),`
|
||
`,e.jsx("img",{src:m,width:"500",style:{margin:"0 0 1rem"}}),`
|
||
`,e.jsx(n.p,{children:"Done!"}),`
|
||
`,e.jsxs(n.h3,{id:"can-i-just-important-it",children:["Can I just... ",e.jsx(n.code,{children:"important!"})," it?"]}),`
|
||
`,e.jsxs(n.p,{children:["Yep! Just be sure your style rule is ",e.jsx(n.strong,{children:"very specific"})," and not something generic that might apply to other elements in your page like ",e.jsx(n.code,{children:"div.container"}),"."]}),`
|
||
`,e.jsxs(n.p,{children:["Many of the components have ",e.jsx(n.code,{children:"class"})," or ",e.jsx(n.code,{children:"id"})," props you can use to attach additional classes or an id that will add more specificity to your components."]})]})}function v(s={}){const{wrapper:n}={...r(),...s.components};return n?e.jsx(n,{...s,children:e.jsx(o,{...s})}):o(s)}export{v as default};
|