--- title: Web basics with Svelte id: 834 html_url: "https://acmcsuf.com/blog/834" discussion_url: "https://github.com/EthanThatOneKid/acmcsuf.com/discussions/834" author: "EthanThatOneKid (https://github.com/EthanThatOneKid)" labels: [] created: "2023-04-09T01:32:53.000Z" edited: "2023-04-11T02:38:28.000Z" --- Web basics with Svelte ====================== 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. > NOTE: It is also important to learn how to read and understand documentation, such as the MDN > documentation [https://developer.mozilla.org/en-US/docs/Web/HTML/Element]. MDN has a standard > format for tables that illustrate compatibility of shared technologies across all browsers > [https://developer.mozilla.org/en-US/docs/MDN/Writing_guidelines/Page_structures/Compatibility_tables]. 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. > NOTE: The browser also understands SVG and WebAssembly, but these are not covered in this > workshop. 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. > NOTE: SvelteKit is a full-stack solution for Svelte (comparable to Next.js, Nuxt.js, and Remix), > but this workshop will focus on using the Svelte compiler to learn HTML fundamentals and Svelte > syntax. 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 * Open a new SvelteKit project (npm create svelte@latest my-app) in VSCode in high-contrast mode. * (Optional) Move files from /my-app/ to the root of your repository. * Run npm run dev to start the development server. * Open the preview in your browser. SET UP SVELTE REPL * Open a new Svelte REPL (https://svelte.dev/repl/ [https://svelte.dev/repl/]). * Sign in to the Svelte REPL to save your work. Remember to save often! SAVE SAVE SAVE! HTML INTRODUCTION > Svelte is a superset of HTML. 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. Hello world! 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.

Hello world!

Ignore lines of code by using the HTML comment tag. In most code editors, the keyboard shortcut is Ctrl + /. 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. The h2 tag is used to indicate the second most important heading on a page. And so on until the h6 tag.

Hello world!

Hello world!

Hello world!

Hello world!

Hello world!
Hello world!
Add a paragraph to your HTML document using the p tag. Line breaks are added to your HTML document using the br tag. > NOTE: p tags cannot be nested inside of other p tags (i.e. a p tag cannot be the child of another > p tag). It is generally not immediately clear why some elements are invalid to nest inside of > other elements, but it is important to follow the rules of HTML by referencing the MDN > documentation [https://developer.mozilla.org/en-US/docs/Web/HTML/Element] when in doubt. > NOTE: A comment is used in the example below as a placeholder for the irrelevant HTML code.

Hello world!

Hello world!

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.

Hello world!

The best programmers 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. 1. Set the ID of the target element via the id attribute. In our case, we will go with title. 2. Set the 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.

Hello world!

The best programmers 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.

Hello world!

The best programmers Add an image to your HTML document using the img tag using the src attribute to set the image source and the alt attribute to set the image's alternative text. > NOTE: The alt attribute is used to provide a textual description of the image. This is useful for > users who are unable to view the image, such as users who are visually impaired. Generally, > carefully choosing the proper semantic HTML tag and attributes will be the difference between a > good user experience and a great user experience. FullyHacks logo 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 all h1 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 [https://developer.mozilla.org/en-US/docs/Web/CSS/named-color#standard_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 [https://developer.mozilla.org/en-US/docs/Web/CSS/Reference]. 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 the li elements, making the li elements children of the ul element. Add an ordered list to your HTML document using the ol tag.
  1. Hello world!
  2. Hello world!
  3. Hello world!
HTML even supports nested lists which can be in any combination of ul and ol tags. > NOTE: Anchor tags are commonly used in li elements to make tables of contents. HTML tables [https://developer.mozilla.org/en-US/docs/Learn/HTML/Tables/Basics] are a common way to display information of all shapes and sizes in a structured way.
Row 1, Column 1 Row 1, Column 2
Row 2, Column 1 Row 2, Column 2
Collect user input in HTML using the input element [https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input]. Customize your input element with more attributes. > NOTE: There are several HTML5 input types > [https://developer.mozilla.org/en-US/docs/Learn/Forms/HTML5_input_types]. Group your inputs in an HTML form. Add a form to your HTML document using the form element [https://developer.mozilla.org/en-US/docs/Learn/Forms]. > NOTE: Presenter opens https://formdata.deno.dev/ [https://formdata.deno.dev/] in a new tab to > demonstrate how forms are used to store user input.
Similarly to anchor tags, forms can be submitted to a web address. Instead of the href attribute, the action attribute is used to specify the web address. The method attribute is used to specify the HTTP method.
> NOTE: For a more comprehensive introduction to conventional HTML document structure, see > https://developer.mozilla.org/en-US/docs/Learn/HTML/Introduction_to_HTML/Document_and_website_structure > [https://developer.mozilla.org/en-US/docs/Learn/HTML/Introduction_to_HTML/Document_and_website_structure]. 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, the isHappy variable is used in the reactive statement, so the statement is run whenever the isHappy variable changes. Code outside of the reactive statement is run once when the component is first rendered. {count} 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 [https://svelte.dev/docs#template-syntax-each]. SVELTE COMPONENTS Svelte components are reusable pieces of code that can be used to build complex user interfaces. > EXERCISE: Make a new Svelte file ending with .svelte and grab any valid Svelte code. For example, > abstract your HTML form into a Svelte component. Import your Svelte component into another Svelte file.
(Optional) Define your own element attributes. In Svelte, component properties are defined using the export keyword. > EXERCISE: For example, you may want to define a name attribute for your form component. 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. > EXERCISE: Make a JavaScript file ending with .js and use the writable function to create a new > Svelte store. Doing so allows you to access the store in any frontend file in your application. import { writable } from "svelte/store"; export const count = writable(0); 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: * Svelte actions [https://svelte.dev/docs#template-syntax-element-directives-use-action] * Svelte slots [https://svelte.dev/docs#template-syntax-slot] * Svelte transitions [https://svelte.dev/docs#run-time-svelte-transition] * SvelteKit documentation [https://kit.svelte.dev/docs] FINAL THOUGHTS We hope you had a great time learning the basics of web development with Svelte in this workshop! SELF LEARNING REFERENCES * Introduction to HTML by MDN [https://developer.mozilla.org/en-US/docs/Learn/HTML/Introduction_to_HTML] * Svelte tutorial [https://svelte.dev/tutorial/basics] * Svelte documentation [https://svelte.dev/docs] ---------------------------------------------------------------------------------------------------- Presented at FullyHacks [https://fullyhacks.acmcsuf.com/] (April 8th, 2023) with <3 by ACM at CSUF [https://acmcsuf.com/] President @KarnikaaVelumani [https://karni.codes/] and Vice President @EthanThatOneKid [https://etok.codes/] Self link: https://acmcsuf.com/basics/ [https://acmcsuf.com/basics/]