Web basics with Svelte #834
EthanThatOneKid
started this conversation in
Newsletter
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Web basics with Svelte
Beginner Svelte workshop for FullyHacks 2023!
Background
What is this workshop about?
This workshop is about web development. Web development is the process of creating websites and web applications. Web development is a broad term that encompasses many different technologies and skills. This workshop will focus on the fundamentals of web development, specifically the fundamentals of Svelte.
What is web development? (What is a website?)
A website is a collection of files that are served to a web browser. The web browser is a program that interprets the files and displays the website to the user. The web browser is the most common way to view websites, so it is important to know the compatibility of the technologies you are using with the web browser that your users are using.
What languages are understood by the browser?
The browser understands HTML, CSS, and JavaScript. HTML is used to structure the content of a website. CSS is used to style the content of a website. JavaScript is used to add interactivity to a website.
Why are frameworks/libraries so prevalent in web development?
Frameworks and libraries are used to reduce the amount of code that needs to be written. This is done by providing a set of pre-written code that can be used to solve common problems. This allows developers to focus on the unique aspects of their project instead of having to write the same code over and over again.
What is Svelte?
Svelte is a powerful tool that compiles Svelte code into optimized HTML, CSS, and JavaScript, making it easy for developers to write efficient code. The Svelte compiler is a powerful tool that allows developers to write code that is easy to read and write, but is also optimized for the browser.
Svelte vs React
Svelte is a compiler for building web interfaces, while React is a library. Svelte compiles components into optimized JavaScript code, resulting in smaller bundle sizes, faster load times, and efficient DOM updates, making it a more optimal choice for performance-oriented web applications. However, React has a larger ecosystem of libraries and community support.
Workshop steps
Open a Svelte development environment
Either set up your Svelte development environment locally with SvelteKit (recommended: VSCode) or the official online Svelte REPL.
Set up VSCode
npm create svelte@latest my-app
) in VSCode in high-contrast mode./my-app/
to the root of your repository.npm run dev
to start the development server.Set up Svelte REPL
HTML introduction
Svelte is a superset of HTML, which means that valid HTML code can also be used in Svelte, including HTML tags and their corresponding semantics.
The HTML tag
In addition to plain text, HTML tags are used to add structure to a web page.
For example, add a heading to your HTML document using the
h1
tag.Ignore lines of code by using the HTML comment tag. In most code editors, the keyboard shortcut is Ctrl + /.
<!-- Hello world! -->
Common HTML tags and semantics
HTML contains rich semantic information that conveys intended meaning to both the browser and readers of your code.
For example, the
h1
tag is used to indicate the most important heading on a page. Theh2
tag is used to indicate the second most important heading on a page. And so on until theh6
tag.Add a paragraph to your HTML document using the
p
tag.Line breaks are added to your HTML document using the
br
tag.Some elements are responsible for behaviors that you'd expect from a web page, such as links and forms.
Add a link to your HTML document using the
a
tag.Test out your anchor tag by clicking on it, but it doesn't work just yet. How come?
The developer is required to set specific attributes that provide the data needed by the browser to behave as desired.
At least two attributes are required for this kind of anchor tag.
id
attribute. In our case, we will go withtitle
.href
attribute to the desired hash. The desired hash is the ID of the target element prefixed with a#
. In our case, it's#title
.Test out your anchor tag by clicking on it, but now it should link to desired HTML element on the page.
Hyperlink to anywhere on the Internet by setting the
href
to your desired web address.Add an image to your HTML document using the
img
tag using thesrc
attribute to set the image source and thealt
attribute to set the image's alternative text.CSS introduction
Svelte looks for CSS in the
style
tag in your Svelte file.Selectors
CSS selectors are used to select the HTML elements that you want to style.
For example, the
h1
selector is used to select allh1
elements.Share CSS styles between multiple HTML elements by using a comma-separated list of selectors.
Properties
CSS properties are used to style HTML elements.
For example, the
color
property is used to set the color of the text. You are encouraged to reference the named CSS colors.There are many CSS properties that can be used to style HTML elements. For a full list of CSS properties, refer to the MDN documentation.
More HTML tags
Lists are a common way to display information in a structured way.
Add an unordered list to your HTML document using the
ul
tag.This showcases the parent-child relationship between HTML elements. Notice how the
ul
element is the parent of theli
elements, making theli
elements children of theul
element.Add an ordered list to your HTML document using the
ol
tag.HTML even supports nested lists which can be in any combination of
ul
andol
tags.HTML tables are a common way to display information of all shapes and sizes in a structured way.
Collect user input in HTML using the
input
element.Customize your input element with more attributes.
Group your inputs in an HTML form. Add a form to your HTML document using the
form
element.Similarly to anchor tags, forms can be submitted to a web address. Instead of the
href
attribute, theaction
attribute is used to specify the web address. Themethod
attribute is used to specify the HTTP method.Svelte superpowers
We emphasize the "super" when we say "Svelte is a superset of HTML" because Svelte adds a few new features to HTML that make it even more powerful.
Conditionals
You may want to display different content depending on the state of your application. For example, you may want to display a loading indicator while data is being fetched from a web API.
Reactivity statements
Svelte allows you to write code that reacts to changes in your application. For example, you may want to change the title of your document depending on the state of your application. The reactive statement is denoted by the
$:
prefix and is run whenever the variables it depends on change. In this case, theisHappy
variable is used in the reactive statement, so the statement is run whenever theisHappy
variable changes. Code outside of the reactive statement is run once when the component is first rendered.Repeating code
It is common to repeat code in HTML documents. For example, you may want to display a list of items. Instead of writing out each item individually, you can use a loop to repeat the code for each item.
More information about loops can be found in the
{#each ...}
template syntax documentation.Svelte components
Svelte components are reusable pieces of code that can be used to build complex user interfaces.
Import your Svelte component into another Svelte file.
(Optional) Define your own element attributes.
In Svelte, component properties are defined using the
export
keyword.Component properties are used in the same way as HTML element attributes.
Svelte stores
Svelte stores are reactive JavaScript variables that can be written to and read from any frontend file in your application.
Access Svelte stores by importing them from the file where they are defined.
Honorable mentions
Svelte and SvelteKit have a lot of features that we didn't have time to cover. Here are some honorable mentions:
Final thoughts
We hope you had a great time learning the basics of web development with Svelte in this workshop!
Self learning references
Presented at FullyHacks (April 8th, 2023) with
<3
by ACM at CSUF President @KarnikaaVelumani and Vice President @EthanThatOneKidSelf link: https://acmcsuf.com/basics/
Beta Was this translation helpful? Give feedback.
All reactions