Judges have ruled that much of the Scottish continuity bill was within Holyrood's competence, barring one section which was not.

Seven justices unanimously held on Thursday that, although the Scottish Bill "as a whole" is not outside Holyrood's powers, the parts of it which would "modify" UK law would be.

The court's president Lady Hale said: "We wish to make it clear that it is no part of our function to determine or to influence the political questions which underlie this dispute.

"Our role is a purely legal one."

Lady Hale explained that the Scottish Bill "provides for the continued validity in Scots law of retained European Union law" and was passed in March, before the UK's EU exit bill.

Law officers were able to refer the bill to the court before it received Royal Assent, so the court could rule on whether it would be within the legislative powers granted to the Scottish Parliament by the Scotland Act.

The judge said Section 17 of the bill - which was successfully challenged by the law officers - had the effect of modifying part of the Scotland Act as it would "render the effect of laws made by the UK Parliament, conferring powers on UK ministers in relation to Scotland, conditional on the consent of the Scottish ministers.

She also said that the UK Parliament's European Union (Withdrawal) Act "changes the legal landscape" and is "protected from modification".

Lady Hale said this prevents the Scottish Bill from "amending, superseding, disapplying or repealing provisions of that Act", and said therefore parts of it would be "outside the legislative competence" of Holyrood.

In the ruling, the justices said: "Withdrawal from the EU will result in legislative powers, which are currently vested in EU institutions, being transferred to institutions within the UK.

"There has been and is a political debate as to which institutions within the UK should best exercise those powers in the public interest.

"It is not the role of this court to form or express any view on those questions of policy, which are the responsibility of our elected representatives and in which the wider civil society has an interest.

"Our role is simply to determine as a matter of law whether and to what extent the Scottish Bill would be within the legislative competence of the Scottish Parliament."

Scottish Brexit Secretary Mike Russell said the UK government had "changed the rules of the game midway through the match" in an "act of constitutional vandalism".

Meanwhile, Scottish Secretary David Mundell said the judgement had provided "much needed legal clarity".

Scottish ministers will issue a formal statement at Holyrood later today.

SNP ministers in Edinburgh brought forward the legislation after branding the UK Government's European Withdrawal Act a "power grab" - fearing this will mean responsibilities they believe should come to Holyrood after Brexit will instead go to Westminster.

When the Bill was passed, Scottish Government ministers insisted it was within Holyrood's competence - although the Scottish Parliament's Presiding Officer Ken Macintosh ruled against them on this.

As a result of the UK Government's legal challenge, the Supreme Court was asked to decide if the UK Withdrawal from the European Union (Legal Continuity) (Scotland Bill) is indeed "within the competence of the Scottish Parliament".

At a hearing in July, a panel of seven justices, including the court's president Lady Hale and deputy president Lord Reed, were urged to find that the legislation "cannot stand".

The issue was referred to the court to seek legal certainty "in the public interest" by the Attorney General and the Advocate General for Scotland, the UK Government's senior law officers.

Advocate General for Scotland Lord Keen told the justices their case was "the Scottish Bill as a whole cannot stand".

He submitted the Bill "impermissibly modifies" the UK Act on withdrawal from the EU.

The UK Bill was given Royal Assent on June 26 and became the European Union (Withdrawal) Act 2018.

The law officers said in their written case before the Supreme Court that the Scottish Bill was passed "without knowledge" of the outcome of negotiations between the UK Government and the EU institutions and "pre-empts them".

They stated: "The effect of what the Scottish Bill does is to make provision for the future relationship with the EU and EU law when that relationship is under negotiation."

They submitted that this "could serve to undermine the credibility of the UK's negotiation and implementation strategy in the eyes of the EU".

As well as hearing the case put forward by the law officers, the justices received submissions in response from Lord Advocate James Wolffe QC.

He argued the justices should rule "in the negative" on the question posed by the law officers relating to whether the Bill "as a whole" is outside the legislative competence of the Scottish Parliament.

He pointed out that although the reference to the court "arises in a politically contentious context, the issues which arise for the court's determination are strictly issues of law".

Wolffe said: "The purpose and effect of the Scottish Bill is to promote legal certainty by making provision for the continuity within the domestic legal system of existing EU-derived law upon and following withdrawal.

"Regardless of any treaty on the future relationship which may be entered into between the UK and the EU, there is a need to provide for legal certainty and continuity when the UK leaves the EU in March 2019, and that is the purpose and effect of the Scottish Bill."

The issue the Supreme Court has been asked to decide is "whether the UK Withdrawal from the European Union (Legal Continuity) (Scotland Bill) is within the competence of the Scottish Parliament".

More to follow.