document is not defined in Astro projext even after adding client:load directive

I had a basic html and css with with vanila javascript which works with keyboard interactions and now transformed to astro but getiing error

document not defined and sugegsted to use client:only

I understand that Astro use SSR so it does not hyderate, but when apply client:load it gives the same error

here is sample code

Panel.astro

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>---
import "assets/styles/panel.css";
import { updatePanel} from "@/components/panel";
---
<label class="vertical" for="vertical">
<input type="radio" class="orientation" name="orientation" id="vertical" value="X">
</label>
</code>
<code>--- import "assets/styles/panel.css"; import { updatePanel} from "@/components/panel"; --- <label class="vertical" for="vertical"> <input type="radio" class="orientation" name="orientation" id="vertical" value="X"> </label> </code>
---
import "assets/styles/panel.css";

import { updatePanel} from "@/components/panel";

---
 <label class="vertical" for="vertical">
      <input type="radio" class="orientation" name="orientation" id="vertical" value="X">
 </label>

components/panle.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const orientationPanel = document.querySelectorAll(".orientation");
const panel = document.querySelector(".panel");
const updatePanel = (num = 0) => {
const fragment = document.createDocumentFragment();
for (let i = 0; i < num; i++) {
const div = document.createElement("div");
div.classList.add("panel__cell");
div.textContent = String.fromCodePoint(65 + i); // A to Z
fragment.appendChild(div);
}
panel.innerHTML = "";
panel.appendChild(fragment);
};
const onChangePanel = (e: any) => {
changePanel(e.target.value);
updatePanel(Number(e.keyCode));
};
orientationPanel?.addEventListener("change", onChangePanel);
</code>
<code>const orientationPanel = document.querySelectorAll(".orientation"); const panel = document.querySelector(".panel"); const updatePanel = (num = 0) => { const fragment = document.createDocumentFragment(); for (let i = 0; i < num; i++) { const div = document.createElement("div"); div.classList.add("panel__cell"); div.textContent = String.fromCodePoint(65 + i); // A to Z fragment.appendChild(div); } panel.innerHTML = ""; panel.appendChild(fragment); }; const onChangePanel = (e: any) => { changePanel(e.target.value); updatePanel(Number(e.keyCode)); }; orientationPanel?.addEventListener("change", onChangePanel); </code>
const orientationPanel = document.querySelectorAll(".orientation");
const panel = document.querySelector(".panel");

const updatePanel = (num = 0) => {
  const fragment = document.createDocumentFragment();
  for (let i = 0; i < num; i++) {
    const div = document.createElement("div");
    div.classList.add("panel__cell");
    div.textContent = String.fromCodePoint(65 + i); // A to Z
    fragment.appendChild(div);
  }
  panel.innerHTML = "";
  panel.appendChild(fragment);
};

const onChangePanel = (e: any) => {
  changePanel(e.target.value);
  updatePanel(Number(e.keyCode));
};

orientationPanel?.addEventListener("change", onChangePanel);

it throw error as below

[ERROR] document is not defined Hint:
Browser APIs are not available on the server.
If the code is in a framework component, try to access these objects after rendering using lifecycle methods
or use a client:only directive to make the component exclusively run on the client

.

so tries to add client:only which has below code

Navigation.astro

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>---
import "@/assets/styles/nav.css";
---
<nav class="nav">
<div class="nav--list">
<a class="link list--item" href="/panel" client:load><span>Panel</span></a>
</div>
</nav>
</code>
<code>--- import "@/assets/styles/nav.css"; --- <nav class="nav"> <div class="nav--list"> <a class="link list--item" href="/panel" client:load><span>Panel</span></a> </div> </nav> </code>
---
import "@/assets/styles/nav.css";
---
<nav class="nav">
  <div class="nav--list">
    <a class="link list--item" href="/panel" client:load><span>Panel</span></a>
  </div>
</nav>

sidenote: when use client:load then throw typescript error on the element

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Type '{ children: any; class: string; href: string; "client:load": true; }' is not assignable to type 'AnchorHTMLAttributes'.
Property 'client:load' does not exist on type 'AnchorHTMLAttributes'.ts(2322)
</code>
<code>Type '{ children: any; class: string; href: string; "client:load": true; }' is not assignable to type 'AnchorHTMLAttributes'. Property 'client:load' does not exist on type 'AnchorHTMLAttributes'.ts(2322) </code>
Type '{ children: any; class: string; href: string; "client:load": true; }' is not assignable to type 'AnchorHTMLAttributes'.
  Property 'client:load' does not exist on type 'AnchorHTMLAttributes'.ts(2322)

it might be because using <a> so create an Astro component

Link.astro

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>---
const { href, name } = Astro.props
---
<a class="link list--item" href={href}><span>{name}</span></a>
</code>
<code>--- const { href, name } = Astro.props --- <a class="link list--item" href={href}><span>{name}</span></a> </code>
---
const { href, name } = Astro.props

---

<a class="link list--item" href={href}><span>{name}</span></a>

and use it in Navigation.astro

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><Link href="panel" name="panel" client:load/>
</code>
<code><Link href="panel" name="panel" client:load/> </code>
<Link href="panel" name="panel" client:load/>

but still throwing same

document is not defined
Hint:
Browser APIs are not available on the server.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật