Week 3 - Advanced Components & Styling

Date

Thursday, October 22, 2026

This week we’ll build on the component concepts from week 2, exploring more advanced ways to structure and style reusable components in Svelte.

Styling

We are going to style components using Tailwind CSS. This is a utility-first CSS framework that allows you to apply styles directly in your HTML or Svelte components using predefined classes.

Code Examples

Link to Tic Tac Toe

Link to Tic Tac Toe Example 2 (from recording)

<script>
    import FreeSquare from "./FreeSquare.svelte";
    import TakenSquare from "./TakenSquare.svelte";
    let board = $state(
        [
            [0, 0, 0],
            [0, 0, 0],
            [0, 0, 0]
        ]
    );
    const isfree = (m, n) => board[m][n] === 0;
    const value = (m , n) => board[m][n];
    let turn = $state(0)
    const nextturn = () => turn = (turn + 1) % 2;
    const set = (m, n) => {
        board[m][n] = turn + 1;
        nextturn();
    }
    const reset = () =>{
        for(let m = 0; m < 3; m++){
            for(let n = 0; n < 3; n++){
                board[m][n] = 0;
            }
        }
    }

</script>
<link rel="stylesheet" href="https://cdn.tailwind-lite.com/2.0.1.css" />

<button onclick={nextturn}>Turn {turn}</button>

<div class="grid grid-cols-3 gap-2 bg-gray-800 p-2 rounded-lg w-64 h-64">
{#each board as row, m}
    {#each row as element, n}
        {#if isfree(m,n)}
            <FreeSquare setFunction={()=>{set(m,n)}} turn={turn}></FreeSquare>
        {:else}
            <TakenSquare value={value(m,n)}></TakenSquare>
        {/if}
    {/each}
{/each}
</div>

<button class="mb-6 px-6 py-2.5 bg-indigo-600 text-white font-semibold rounded-lg shadow-md hover:bg-indigo-700 hover:shadow-lg active:scale-95 transition-all duration-200 cursor-pointer" onclick={() => reset()}>Reset</button>
<script>
    const {setFunction, turn} = $props();
</script>


<button 
    class="w-full h-full bg-white text-6xl flex items-center justify-center cursor-pointer hover:bg-gray-200 text-white"
    onclick={setFunction}>
    {#if turn===0}
        O
    {:else}
        X
    {/if}
</button>
<script>
    const {value} = $props();
</script>

{#if value === 1}
    <div class="w-full h-full bg-white text-6xl flex items-center justify-center text-blue-500">
        X
    </div>
{/if}
{#if value === 2}
    <div class="w-full h-full bg-white text-6xl flex items-center justify-center text-red-500">
        O
    </div>
{/if}

State Diagram

flowchart LR
    Start([Start Game]) --> P1[Player 1: Place 'X']
    P1 --> Check1{3 in a row\nor Board full?}
    Check1 -- No --> P2[Player 2: Place 'O']
    Check1 -- Yes --> End([Game Over])
    P2 --> Check2{3 in a row\nor Board full?}
    Check2 -- No --> P1
    Check2 -- Yes --> End

Exercise

Currently the hover over for the tic tac toe board shows the incorrect (nought or cross) value. Change the code to fix this.

The board will not currently stop if there is a three in a row. Change the code to stop the game if there is a winner.

Try to style the board different if there is a winner.

Webinar Recording

The webinar recording will be available during the following week.

Slides

link to slides