|
| 1 | +- Start Date: 2018-11-05 |
| 2 | +- RFC PR: |
| 3 | +- Svelte Issue: |
| 4 | + |
| 5 | +### THINGS TO WATCH: |
| 6 | + |
| 7 | +https://www.youtube.com/watch?v=lK3OiJvwgSc |
| 8 | +https://github.com/GoogleChromeLabs/houdini-samples/blob/master/layout-worklet/masonry/index.html |
| 9 | +http://ishoudinireadyyet.com/ |
| 10 | +https://adamwathan.me/css-utility-classes-and-separation-of-concerns/ |
| 11 | + |
| 12 | + |
| 13 | +### WIP from the mouth of Rich: |
| 14 | + |
| 15 | +* modularity. we need a way to import CSS that the compiler understands, and we need a way to compose styles a la modular-css |
| 16 | +* optimisation. at present, the only form of optimisation is (incomplete) minification |
| 17 | +* as you put the RFC together for the dynamic parts, I'd urge you to think about implementation approaches and how they affect the design. For example, if I have a class like .foo and some markup like <div class={passedInFromOutside}> the compiler doesn't know that class="foo" could be the result, so it becomes incredibly hard to know that it needs to apply particular dynamic styles to that element |
| 18 | +* (if the implementation calls for CSS variables, then we should probably just use CSS variables in dynamic style tags, and be explicit about the whole thing) |
| 19 | +* I'd also suggest studying how theming is applied to custom elements with shadow DOM (I'm not sure what the current state of the art is, I only know that shadow-piercing selectors were deprecated). This is for two reasons: 1) they will have already discussed and anticipated a lot of the problems we would likely face, and 2) it makes it more likely that we're able to reuse the solution when compiling to custom elements(edited) |
| 20 | +* For the optimisation piece, it's worth looking at OptiCSS and thinking about that work could apply to ours (Svelte could even use OptiCSS directly, perhaps). That will likely have ramifications for the compiler API (since it needs to think beyond single components), so it touches on the question of whole-app optimisation |
| 21 | +* CSS Shadow Parts https://drafts.csswg.org/css-shadow-parts/ |
| 22 | + |
| 23 | +# CSS-in-HTML |
| 24 | + |
| 25 | +## Summary |
| 26 | + |
| 27 | +This is a larger RFC focused on solving CSS scoping on both a stand-alone component and global level. This RFC also proposes an approach to reactive CSS as well as better tooling for incorporating 3rd party processors and compilers. |
| 28 | + |
| 29 | +## Motivation |
| 30 | + |
| 31 | +Svelte developers generally acknowledge that there needs to be a better answer for CSS both in components as well as a larger ecosystem consideration. There are varities of opinions about best methodologies for CSS. This proposal sidesteps opinions as much as possible, allowing developers to work with whatever framework or method best suits their purposes, yet cherrypicking the best design considerations. |
| 32 | + |
| 33 | +One debate often boils down to scoping considerations. Currently, with Svelte 2, the inclusion of any CSS within a style tag is considered 100% scoped. Consider: |
| 34 | + |
| 35 | +```html |
| 36 | +<!-- main component --> |
| 37 | +<div> |
| 38 | + <CascadingDiv/> |
| 39 | +</div> |
| 40 | +<style> |
| 41 | + div { color: goldenrod; } |
| 42 | +</style> |
| 43 | +<script> |
| 44 | +export default { |
| 45 | + components: { CascadingDiv: './cascasing-div.html' } |
| 46 | +}; |
| 47 | +</script> |
| 48 | +``` |
| 49 | + |
| 50 | +```html |
| 51 | +<!-- component: cascasing-div.html <CascadingDiv/> --> |
| 52 | +<div>This is not goldenrod. Styles do not cascade!</div> |
| 53 | +``` |
| 54 | + |
| 55 | +The only way out of this is to wrap your selector(s) in `:global()`. This, however, gets to be fairly verbose when you want to wrap all selectors on the page. |
| 56 | + |
| 57 | +```html |
| 58 | +<style> |
| 59 | +:global(.foo) { text-align: center; } |
| 60 | +:global(.foo span) { font-size: 80%; } |
| 61 | +:global(.foo em) { color: pink; } |
| 62 | +.bar { text-align: right; } |
| 63 | +<!-- unscoped: `.foo *` --> |
| 64 | +<!-- scoped: `.bar` --> |
| 65 | +</style> |
| 66 | +``` |
| 67 | +
|
| 68 | +Arguably, the appeal of single file components is encapsulating all component-relatives into the single file. But the above also has the sometimes unwanted effect of cascading everywhere. This presents the complex challenge of controlling scope to a single component, a set of embedded components, or global. |
| 69 | +
|
| 70 | +It would be nice if there was a simple mechanism for controlling scope more easily at a component level, making JS variables behave dynamically in CSS and HTML, as well as passing style scopes from component to embedded component if necessary. |
| 71 | +
|
| 72 | +## Detailed design |
| 73 | +
|
| 74 | +> This is the bulk of the RFC. |
| 75 | +
|
| 76 | +> Explain the design in enough detail for somebody |
| 77 | +familiar with the framework to understand, and for somebody familiar with the |
| 78 | +implementation to implement. This should get into specifics and corner-cases, |
| 79 | +and include examples of how the feature is used. Any new terminology should be |
| 80 | +defined here. |
| 81 | +
|
| 82 | +## How we teach this |
| 83 | +
|
| 84 | +> What names and terminology work best for these concepts and why? How is this |
| 85 | +idea best presented? As a continuation of existing Svelte patterns, or as a |
| 86 | +wholly new one? |
| 87 | +
|
| 88 | +> Would the acceptance of this proposal mean the Svelte guides must be |
| 89 | +re-organized or altered? Does it change how Svelte is taught to new users |
| 90 | +at any level? |
| 91 | +
|
| 92 | +> How should this feature be introduced and taught to existing Svelte |
| 93 | +users? |
| 94 | +
|
| 95 | +## Drawbacks |
| 96 | +
|
| 97 | +> Why should we *not* do this? Please consider the impact on teaching Svelte, |
| 98 | +on the integration of this feature with other existing and planned features, |
| 99 | +on the impact of the API churn on existing apps, etc. |
| 100 | +
|
| 101 | +> There are tradeoffs to choosing any path, please attempt to identify them here. |
| 102 | +
|
| 103 | +## Alternatives |
| 104 | +
|
| 105 | +> What other designs have been considered? What is the impact of not doing this? |
| 106 | +
|
| 107 | +> This section could also include prior art, that is, how other frameworks in the same domain have solved this problem. |
| 108 | +
|
| 109 | +## Unresolved questions |
| 110 | +
|
| 111 | +> Optional, but suggested for first drafts. What parts of the design are still |
| 112 | +TBD? |
0 commit comments